62,041
社区成员
发帖
与我相关
我的任务
分享
public void SaveDetailToExcel(DataSet ds, string filePath)
{
try
{
GC.Collect();
Application app;
Worksheet wSheet = null;
Workbook wBook;
app = new ApplicationClass();
app.Visible = false;
wBook = app.Workbooks.Add(true);
int t = ds.Tables.Count;
for (int sheetIndex = 0; sheetIndex < t; sheetIndex++)
{
wSheet = (Worksheet)wBook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
wSheet.Name = ds.Tables[sheetIndex].TableName;
//赋值
if (ds.Tables[sheetIndex].Rows.Count > 0)
{
int row = 0;
row = ds.Tables[sheetIndex].Rows.Count;
int col = ds.Tables[sheetIndex].Columns.Count;
for (int i = 3; i <= row + 2; i++)
{
for (int j = 2; j < col; j++)
{
string str = ds.Tables[sheetIndex].Rows[i - 3][j].ToString();
wSheet.Cells[i + 1, j - 1] = str;
}
}
}
wSheet.Cells[1, 1] = "表";
//设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
}
wBook.Save();
app.Save(filePath);
wBook.Close(false, null, null);
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wSheet);
wBook = null;
app = null;
wSheet = null;
GC.Collect();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}