我必須創建一個將數據集導出到Excel的控制檯應用程序。我試着用Google搜索並查看關於這個主題的帖子,但還沒有真正找到我要找的東西。問題是它不應該彈出保存窗口,它應該自動創建excel文件。到目前爲止,我有以下代碼,但我不知道如何使它自動保存。將不勝感激任何幫助。Excel interop:在不顯示保存對話框的情況下保存工作簿
public static void CreateWorkbook(DataSet ds, String path)
{
int rowindex = 0;
int columnindex = 0;
Microsoft.Office.Interop.Excel.Application wapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet wsheet;
Microsoft.Office.Interop.Excel.Workbook wbook;
wapp.Visible = false;
wbook = wapp.Workbooks.Add(true);
wsheet = (Worksheet)wbook.ActiveSheet;
try
{
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
wsheet.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
foreach (DataRow row in ds.Tables[0].Rows)
{
rowindex++;
columnindex = 0;
foreach (DataColumn col in ds.Tables[0].Columns)
{
columnindex++;
wsheet.Cells[rowindex + 1, columnindex] = row[col.ColumnName];
}
}
}
catch (Exception ex)
{
String err = ex.Message;
}
wapp.UserControl = true;
}
謝謝查爾斯,那工作:) – ha1ogen