4,816
社区成员
发帖
与我相关
我的任务
分享
#region 导出到excel
/// <summary>
/// 导出到excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExportExcel_Click(object sender, EventArgs e)
{
try
{
System.Data.DataTable dt = dtSource;
string TemplatePath = Server.MapPath(@"Template\Third.xls");
string ExportPath = Server.MapPath("") + @"\ExportReports";
if (!Directory.Exists(ExportPath))
{
Directory.CreateDirectory(ExportPath);
}
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
if (app == null)
{
return;
}
app.Visible = false;
app.UserControl = true;
Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks;
Microsoft.Office.Interop.Excel._Workbook workbook = workbooks.Add(TemplatePath);
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets;
Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
return;
}
int i = 1;
for (; i <= dt.Rows.Count; i++)
{
int j = 0;
worksheet.Cells[i + 2, ++j] = dt.Rows[i - 1]["Month"].ToString().Trim();
string str = DateTime.Now.ToString("yyyyMMddHHmmss");
string strExportPath = ExportPath + @"\" + str + ".xls";
workbook.SaveAs(strExportPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
app.Quit();
app = null;
Response.Redirect("DownLoad.aspx?Path=" + strExportPath, false);
}
catch
{
Response.Write("<script>alert('导出失败!!')</script>");
return;
}
}
#endregion
book=(Excel._Workbook)app.Workbooks.Open(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
Excel.Application app = new Application();
Excel.Application app=new Application();
using Excel;
根本不起作用