首先肯定是先引用NPOI组件,可以下载dll文件或者在nuget管理器里安装,以下为具体源码:
DataSet ds = Db.ExeDataSet(sql);//从数据库获取到datatable原始数据 string[] excelColumnName = new string[] { "编号", "来源", "类型", "内容", "地址", "上报人", "时间", "更新时间", "程度", "人员" };//定义表头字段数组 IWorkbook wk = new HSSFWorkbook(); ISheet sheet = wk.CreateSheet("work"); IRow rowCloumn = sheet.CreateRow(0); DataTable data = ds.Tables[0]; HSSFCellStyle cellStyle = (HSSFCellStyle)wk.CreateCellStyle(); //创建列头样式 cellStyle.Alignment = HorizontalAlignment.Center; //水平居中 ///表头 根据上面定义好的表头创建表头 for (int i = 0; i < excelColumnName.Length; i++) { sheet.SetDefaultColumnStyle(i, cellStyle);//设置当前列的默认样式 rowCloumn.CreateCell(i).SetCellValue(excelColumnName[i]);//设置当前列的单元格文本(表头名称) sheet.SetColumnWidth(i, 20*256);//设置每一列的宽度 } var length=excelColumnName.Length;//这里是拿到列的总数 HSSFCellStyle cellStyle1 = (HSSFCellStyle)wk.CreateCellStyle(); //创建列头样式 cellStyle1.Alignment = HorizontalAlignment.Center; //水平居中 //创建字体 HSSFFont ffont = (HSSFFont)wk.CreateFont(); //给字体设置颜色 ffont.Color = HSSFColor.Blue.Index; //给样式添加字体 cellStyle1.SetFont(ffont); sheet.SetDefaultColumnStyle(length, cellStyle1); rowCloumn.CreateCell(length++).SetCellValue("图片1");//在上面的基础上 再创建一列 列名为图片1 并设置默认样式 sheet.SetDefaultColumnStyle(length, cellStyle1); rowCloumn.CreateCell(length++);//再创建一列 本列没有设置单元格名称 sheet.SetDefaultColumnStyle(length, cellStyle1); rowCloumn.CreateCell(length++).SetCellValue("图片2");//在上面的基础上 再创建一列 列名为图片2 并设置默认样式 sheet.SetDefaultColumnStyle(length, cellStyle1);//再创建一列 本列没有设置单元格名称 rowCloumn.CreateCell(length++); sheet.AddMergedRegion(new CellRangeAddress(0, 0, 10, 11));//合并10列和11列单元格 为一格 sheet.AddMergedRegion(new CellRangeAddress(0, 0, 12, 13));//合并12列和13列单元格 为一格 ///数据内容 for (int i = 0; i < data.Rows.Count; i++) { IRow row1 = sheet.CreateRow(i + 1); for (int j = 0; j < data.Columns.Count-2; j++) { ICell cell = row1.CreateCell(j); cell.SetCellValue(data.Rows[i][j].ToString()); } var arr1 = string.IsNullOrWhiteSpace(data.Rows[i]["ImgUrls"].ToString())?new List<string>().ToArray(): data.Rows[i]["ImgUrls"].ToString().Split('|'); var len = data.Columns.Count - 2; string url1 = "", url2 = "", url3 = "", url4 = ""; if(arr1.Length>0) { url1 = arr1[0]; url2 = arr1.Length > 1 ? arr1[1] : ""; } var prefix = System.Configuration.ConfigurationManager.AppSettings["domain"]; SetExcelImgUlrLink(len++, url1, "图1", prefix, row1); SetExcelImgUlrLink(len++, url2, "图2", prefix, row1); var arr2 = string.IsNullOrWhiteSpace(data.Rows[i]["DoneImgUrls"].ToString()) ? new List<string>().ToArray() : data.Rows[i]["DoneImgUrls"].ToString().Split('|'); if (arr2.Length > 0) { url3 = arr2[0]; url4 = arr2.Length > 1 ? arr2[1] : ""; } SetExcelImgUlrLink(len++, url3, "图1", prefix, row1); SetExcelImgUlrLink(len++, url4, "图2", prefix, row1); } string excelName = Guid.NewGuid().ToString() + ".xls"; string directory = "/uploadFile/"; string savePath = AppDomain.CurrentDomain.BaseDirectory + directory; using (FileStream fs = new FileStream(savePath + excelName, FileMode.Create)) { wk.Write(fs); fs.Close(); } string fileName =directory + excelName;
private void SetExcelImgUlrLink(int len, string url,string display,string prefix,IRow row1) { //设置超链接 if (!string.IsNullOrWhiteSpace(url)) { ICell cell2 = row1.CreateCell(len); cell2.SetCellValue(display); HSSFHyperlink link1 = new HSSFHyperlink(HyperlinkType.Url); link1.Address = prefix + url; cell2.Hyperlink = link1; } }