public string 导出Excel(string filename, params System.Data.DataTable[] dts)
{
string FilePath = System.Windows.Forms.Application.StartupPath + @"\x\" + filename + ".xlsx";
if (!System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath + @"\x"))
{
System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + @"\x");
}
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = null;
for (int index = 0; index < dts.Length; index++)
{
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, wb.Worksheets.get_Item("Sheet1"), Type.Missing, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
ws.Name = dts[index].TableName;
Microsoft.Office.Interop.Excel.Range ra;
int index_row, index_col;
int jump = 0;
ws.Columns.NumberFormatLocal = "@";
for (index_col = 0; index_col < dts[index].Columns.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + 1];
ra.set_Value(null,
dts[index].Columns[index_col].ColumnName
);
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + jump + 1];
jump++;
}
}
for (index_row = 0; index_row < dts[index].Rows.Count; index_row++)
{
for (index_col = 0; index_col < dts[index].Columns.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];
ra.set_Value(null,
dts[index].Rows[index_row][index_col].ToString()
);
}
}
ws.Columns.EntireColumn.AutoFit();
}
if ((Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets.get_Item("Sheet1") != null)
((Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets.get_Item("Sheet1")).Delete();
wb.Saved = true;
wb.SaveCopyAs(FilePath);
wb.Close(true, FilePath, null);
app.Quit();
/*
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("et.exe"))
{
//先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,
//但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它:p
if (theProc.CloseMainWindow() == false)
{
theProc.Kill();
}
}
app = null;
*/
string _return = FilePath;
GC.Collect();
return _return;
}
{
string FilePath = System.Windows.Forms.Application.StartupPath + @"\x\" + filename + ".xlsx";
if (!System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath + @"\x"))
{
System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + @"\x");
}
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = null;
for (int index = 0; index < dts.Length; index++)
{
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, wb.Worksheets.get_Item("Sheet1"), Type.Missing, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
ws.Name = dts[index].TableName;
Microsoft.Office.Interop.Excel.Range ra;
int index_row, index_col;
int jump = 0;
ws.Columns.NumberFormatLocal = "@";
for (index_col = 0; index_col < dts[index].Columns.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + 1];
ra.set_Value(null,
dts[index].Columns[index_col].ColumnName
);
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + jump + 1];
jump++;
}
}
for (index_row = 0; index_row < dts[index].Rows.Count; index_row++)
{
for (index_col = 0; index_col < dts[index].Columns.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];
ra.set_Value(null,
dts[index].Rows[index_row][index_col].ToString()
);
}
}
ws.Columns.EntireColumn.AutoFit();
}
if ((Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets.get_Item("Sheet1") != null)
((Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets.get_Item("Sheet1")).Delete();
wb.Saved = true;
wb.SaveCopyAs(FilePath);
wb.Close(true, FilePath, null);
app.Quit();
/*
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("et.exe"))
{
//先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,
//但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它:p
if (theProc.CloseMainWindow() == false)
{
theProc.Kill();
}
}
app = null;
*/
string _return = FilePath;
GC.Collect();
return _return;
}