日期:2014-05-20 浏览次数:21005 次
string filePath = "";
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
filePath = saveFileDialog1.FileName;
Microsoft.Office.Interop.Excel.Application app =
new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
app.Visible = false;
Workbook wBook = app.Workbooks.Add(true);
Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
if (inventList.Count>0)
{
int row = 0;
row = inventList.Count;
for (int i = 0; i < row; i++)
{
wSheet.Cells[i+2, 1] = inventList[i].PartNo;
}
wSheet.Cells[1, 1] = "产品编号";
}
//设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
//app.Workbooks[1].Close(true, filePath, null);
app.Workbooks[1].Save();
app.Save(filePath);
app.SaveWorkspace(filePath);
app.Quit();
app = null;
MessageBox.Show("数据导出成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("导出数据时出现错误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}