意:这里借助到了两个第三方的插件。
见附件地址:https://files.cnblogs.com/files/JackZhangcom/NPOI_DLL.zip
1.注册按钮:
((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(btnExport);
2.在刚才的注册的进行导出excle代码
protected void btnExport_Click(object sender, EventArgs e)
{
downLoadExcle(;)
}
代码如下:
public void downLoadExcle() { HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个sheet ISheet sheet1 = workbook.CreateSheet("sheet1"); // 设置列宽,excel列宽每个像素是1/256 sheet1.SetColumnWidth(0, 18 * 256); sheet1.SetColumnWidth(1, 18 * 256); sheet1.SetColumnWidth(2, 18 * 256); sheet1.SetColumnWidth(3, 18 * 256); sheet1.SetColumnWidth(4, 18 * 256); IRow rowHeader = sheet1.CreateRow(0);//创建表头行 rowHeader.CreateCell(0, CellType.String).SetCellValue("咨询类别"); rowHeader.CreateCell(1, CellType.String).SetCellValue("咨询名称"); rowHeader.CreateCell(2, CellType.String).SetCellValue("咨询内容"); rowHeader.CreateCell(3, CellType.String).SetCellValue("提交时间"); rowHeader.CreateCell(4, CellType.String).SetCellValue("图片"); string strWhere = "";//兼顾sql与oracle int TotalNum = 0; DataTable MyDataTable = new BaseCode.DataBizlogic().GetDLJListPage(DataGrid1.PageSize, DataGrid1.CurrentPageIndex + 1, out TotalNum, strWhere); //MyDataTable.Columns.Add(new DataColumn("Pic", typeof(string))); DataTable dt = MyDataTable; if (DataGrid1.GetCheckedRows().Count == 0) { dt = new BaseCode.DataBizlogic().GetDLJAllListPage(); } if (DataGrid1.GetCheckedRows().Count <= DataGrid1.PageSize && DataGrid1.GetCheckedRows().Count!=0) //导出本页选中的数据 { int iRow = 0; foreach (string RowGuid in DataGrid1.GetCheckedRows()) { for (int i = 0; i < dt.Rows.Count; i++) { if (RowGuid == dt.Rows[i]["RowGuid"].ToString()) { IRow row = sheet1.CreateRow(iRow + 1); //设置行高 ,excel行高度每个像素点是1/20 row.Height = 80 * 20; //填入生产单号 row.CreateCell(0, CellType.String).SetCellValue(dt.Rows[i]["zxType"].ToString()); row.CreateCell(1, CellType.String).SetCellValue(dt.Rows[i]["zxName"].ToString()); row.CreateCell(2, CellType.String).SetCellValue(dt.Rows[i]["zxContent"].ToString()); row.CreateCell(3, CellType.String).SetCellValue(dt.Rows[i]["InfoDate"].ToString()); //将图片文件读入一个字符串 //byte[] bytes = System.IO.File.ReadAllBytes(datarow["KTL_PIC"].ToString()); string strPicPath = ""; strPicPath += ConfigurationManager.AppSettings["PicPath"].ToString(); string InfoRowGuid = dt.Rows[i]["RowGuid"].ToString(); //获得该条信息的RowGuid DataTable dTableInfoUpfile = new DataBizlogic().GetInfoUpfile(InfoRowGuid); if (dTableInfoUpfile != null && dTableInfoUpfile.Rows.Count > 0) { string AttachID = dTableInfoUpfile.Rows[0]["AttachID"].ToString(); strPicPath += (@"/" + AttachID + @"/" + dTableInfoUpfile.Rows[0]["filename"].ToString()); Uri url = new Uri(strPicPath); WebRequest webRequest = WebRequest.Create(url); WebResponse webResponse = webRequest.GetResponse(); if (webResponse != null) { Bitmap myImage = new Bitmap(webResponse.GetResponseStream()); MemoryStream mestr = new MemoryStream(); myImage.Save(mestr, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] bytes = mestr.ToArray(); //byte[] bytes = System.IO.File.ReadAllBytes(@"F:\Job\test\01.JPG"); int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch(); // 插图片的位置 HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释 HSSFClientAnchor anchor = new HSSFClientAnchor(70, 10, 0, 0, 4, iRow + 1, 5, (iRow + 1) + 1); //把图片插到相应的位置 HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx); } } iRow++; } } } } else //导出所有的数据 { if (dt.Rows.Count > 0) { int rowline = 1;//从第二行开始(索引从0开始) foreach (DataRow datarow in dt.Rows) { IRow row = sheet1.CreateRow(rowline); //设置行高 ,excel行高度每个像素点是1/20 row.Height = 80 * 20; //填入生产单号 row.CreateCell(0, CellType.String).SetCellValue(datarow["zxType"].ToString()); row.CreateCell(1, CellType.String).SetCellValue(datarow["zxName"].ToString()); row.CreateCell(2, CellType.String).SetCellValue(datarow["zxContent"].ToString()); row.CreateCell(3, CellType.String).SetCellValue(datarow["InfoDate"].ToString()); //将图片文件读入一个字符串 //byte[] bytes = System.IO.File.ReadAllBytes(datarow["KTL_PIC"].ToString()); string strPicPath = ""; strPicPath += ConfigurationManager.AppSettings["PicPath"].ToString(); string InfoRowGuid = datarow["RowGuid"].ToString(); //获得该条信息的RowGuid DataTable dTableInfoUpfile = new DataBizlogic().GetInfoUpfile(InfoRowGuid); if (dTableInfoUpfile != null && dTableInfoUpfile.Rows.Count > 0) { string AttachID = dTableInfoUpfile.Rows[0]["AttachID"].ToString(); strPicPath += (@"/" + AttachID + @"/" + dTableInfoUpfile.Rows[0]["filename"].ToString()); //byte[] bytes = System.IO.File.ReadAllBytes(@"F:\Job\test\01.JPG"); byte[] bytes = System.IO.File.ReadAllBytes(strPicPath); int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch(); // 插图片的位置 HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释 HSSFClientAnchor anchor = new HSSFClientAnchor(70, 10, 0, 0, 4, rowline, 5, rowline + 1); //把图片插到相应的位置 HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx); } rowline++; } } } ////把文件保存到d:\aaa.xls,注意扩展名是.xls不要写成.xlsx //using (Stream stream = File.OpenWrite(@"F:\aaa.xls")) //{ // workbook.Write(stream); //} //使用文件流保存 MemoryStream ms = new MemoryStream(); workbook.Write(ms); string ExclePath = System.Configuration.ConfigurationManager.AppSettings["ExclePath"].ToString(); string tempExcel = Path.Combine(ExclePath, @"ExcelExport\TestExcel.xls"); using (FileStream fs = new FileStream(tempExcel, FileMode.Create, FileAccess.ReadWrite)) { ms.WriteTo(fs); } Response.Clear(); Response.ClearHeaders(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachent;filename=" + HttpUtility.UrlDecode(@"TestExcel.xls"));//TestExcel.xls可修改 Response.WriteFile(tempExcel, true); Response.Flush(); Response.Close(); if (File.Exists(tempExcel)) { File.Delete(tempExcel); } Response.End(); }