使用插件实现一般处理程序导出excel
string sql = "select * from WJ_ProjectManager where" + WhereString ;
DataTable dt = SqlHelper.ExecuteDataTable(sql);
Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
string excelFile = string.Empty;
excelFile = "项目管理导出模板.xlsx";
wk.Open(System.Web.HttpContext.Current.Server.MapPath("/View/ProjectManager/" + excelFile));
if (dt.Rows.Count > 0) {
for (int i = 0; i < dt.Rows.Count; i++)
{
wk.Worksheets[0].Cells[i + 2, 0].PutValue(i+1); //项目名称
wk.Worksheets[0].Cells[i + 2, 1].PutValue(dt.Rows[i]["P_Name"]); //项目名称
wk.Worksheets[0].Cells[i + 2, 2].PutValue(dt.Rows[i]["P_Customer"].ToString().Split(',')[1]); //关联客户
wk.Worksheets[0].Cells[i + 2, 3].PutValue(dt.Rows[i]["P_Price"]); //项目价格
wk.Worksheets[0].Cells[i + 2, 4].PutValue(dt.Rows[i]["P_StartDay"].ToString().Substring(0,10)); //预计实施时间
wk.Worksheets[0].Cells[i + 2, 5].PutValue(dt.Rows[i]["P_AddDay"].ToString().Substring(0,10));//项目新增时间
wk.Worksheets[0].Cells[i + 2, 6].PutValue(dt.Rows[i]["P_Status"]);//项目状态
wk.Worksheets[0].Cells[i + 2, 7].PutValue(dt.Rows[i]["P_Relation"]);//联系人
wk.Worksheets[0].Cells[i + 2, 8].PutValue(dt.Rows[i]["P_Solder"]);//销售人员
}
string yxdatet = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Millisecond;
string filename = yxdatet + "项目管理.xls";
string serverPath = System.Web.HttpContext.Current.Server.MapPath("/import/" + filename);
wk.Save(serverPath);
FileInfo fileInfo = new FileInfo(serverPath);
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Charset = "GB2312";
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode(yxdatet + "项目管理.xls"));
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.AddHeader("Content-Transfer-Encoding", "binary");
context.Response.ContentType = "application/octet-stream";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
context.Response.WriteFile(fileInfo.FullName);
context.Response.Flush();
context.Response.End();