意:這里借助到了兩個第三方的插件。
見附件地址: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(); }