#region 寫入excle,dt爲要寫入excle的dataTable
Microsoft.Office.Interop.Excel.Application app =
new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wBook = app.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet wSheet = wBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
if (dt.Rows.Count > 0)
{
int row = 0;
row = dt.Rows.Count;
int col = dt.Columns.Count;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
string str = dt.Rows[i][j].ToString();
wSheet.Cells[i + 2, j + 1] = str;
}
}
}
int size = dt.Columns.Count;
for (int i = 0; i < size; i++)
{
wSheet.Cells[1, 1 + i] = dt.Columns[i].ColumnName;
}
string filename = System.DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx"; //"/File/" +
string filepath ="C:\\ScheduledJob\\weeklyreport\\"+filename; //System.Web.HttpContext.Current.Server.MapPath(filename);
//設置禁止彈出保存和覆蓋的詢問提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
//保存工作簿
// wBook.Save();
wBook.Saved = true;
wBook.SaveCopyAs(filepath);
//保存excel文件
// app.Save(filepath);
// app.SaveWorkspace(filepath);
app.Quit();
app = null;
#endregion