NPOI操作Excel,NPOI畫較複雜表格和設定其單元格格式
阿新 • • 發佈:2019-01-10
在最近的專案中,又使用了NPOI把資料匯出到Excel,發現新版的使用方法和舊版的還是有些區別,而且貌似新版NPOI功能更強大了,在這個專案裡面,主要遇到了兩個問題,最後成功解決了,那麼下面的也就是列出了部分程式碼,程式碼中列出瞭如何解決如下問題:
1、如何畫結構相對複雜點的表結構
2、如何設定表中單元格的格式
(1)、在HTML中畫複雜點的表格
<table width="100%" border="1" cellspacing="0" cellpadding="0" id="tbList"> <tr> <td colspan="13"> <div id="position"> </div> </td> </tr> <tr> <td colspan="4"> 單位面積:畝 </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td rowspan="3"> 行政區程式碼 </td> <td rowspan="3"> 行政區名稱 </td> <td rowspan="2" colspan="2"> 合計 </td> <td colspan="4"> 國有土地 </td> <td colspan="4"> 集體土地 </td> </tr> <tr> <td colspan="2"> 國有土地所有權 </td> <td colspan="2"> 國有土地使用權 </td> <td colspan="2"> 集體土地所有權 </td> <td colspan="2"> 集體土地使用權 </td> </tr> <tr> <td> 登記數量 </td> <td> 宗地面積 </td> <td> 登記數量 </td> <td> 宗地面積 </td> <td> 登記數量 </td> <td> 宗地面積 </td> <td> 登記數量 </td> <td> 宗地面積 </td> <td> 登記數量 </td> <td> 宗地面積 </td> </tr> </table>
(2)在使用NPOI匯入到Excel中如何畫:
void GenerateData(object[] objArr,string xzqMc) { ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); sheet1.SetColumnWidth(0, 4000); sheet1.SetColumnWidth(1, 4000); sheet1.SetColumnWidth(2, 3000); sheet1.SetColumnWidth(3, 3000); sheet1.SetColumnWidth(4, 3000); sheet1.SetColumnWidth(5, 3000); sheet1.SetColumnWidth(6, 3000); sheet1.SetColumnWidth(7, 3000); sheet1.SetColumnWidth(8, 3000); sheet1.SetColumnWidth(9, 3000); sheet1.SetColumnWidth(10, 3000); sheet1.SetColumnWidth(11, 3000); IRow row0 = sheet1.CreateRow(0); row0.HeightInPoints = 30; IFont fontTitle = hssfworkbook.CreateFont(); fontTitle.FontHeightInPoints = 12; fontTitle.Boldweight = (short)FontBoldWeight.BOLD; ICellStyle styleTiltle = hssfworkbook.CreateCellStyle(); styleTiltle.Alignment = HorizontalAlignment.CENTER; styleTiltle.SetFont(fontTitle); styleTiltle.VerticalAlignment = VerticalAlignment.CENTER; IFont fontContent = hssfworkbook.CreateFont(); fontContent.FontHeightInPoints = 12; ICellStyle styleContent = hssfworkbook.CreateCellStyle(); styleContent.Alignment = HorizontalAlignment.CENTER; styleContent.SetFont(fontContent); styleContent.VerticalAlignment = VerticalAlignment.CENTER; string currxzq = xzqMc+"省(市、縣)土地登記資訊彙總表"; row0.CreateCell(0).SetCellValue(currxzq); sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 11)); row0.GetCell(0).CellStyle = styleTiltle; IRow row1 = sheet1.CreateRow(1); row1.CreateCell(0).SetCellValue("面積單位:畝"); sheet1.AddMergedRegion(new CellRangeAddress(1, 1, 0, 3)); row1.GetCell(0).CellStyle = styleContent; IRow row2 = sheet1.CreateRow(2); row2.CreateCell(0).SetCellValue("行政區程式碼"); sheet1.AddMergedRegion(new CellRangeAddress(2, 4, 0, 0)); row2.GetCell(0).CellStyle = styleContent; row2.CreateCell(1).SetCellValue("行政區名稱"); sheet1.AddMergedRegion(new CellRangeAddress(2, 4, 1, 1)); row2.GetCell(1).CellStyle = styleContent; row2.CreateCell(2).SetCellValue("合計"); sheet1.AddMergedRegion(new CellRangeAddress(2, 3, 2, 3)); row2.GetCell(2).CellStyle = styleContent; row2.CreateCell(4).SetCellValue("國有土地"); sheet1.AddMergedRegion(new CellRangeAddress(2, 2, 4, 7)); row2.GetCell(4).CellStyle = styleContent; row2.CreateCell(8).SetCellValue("集體土地"); sheet1.AddMergedRegion(new CellRangeAddress(2, 2, 8, 11)); row2.GetCell(8).CellStyle = styleContent; IRow row3 = sheet1.CreateRow(3); row3.CreateCell(4).SetCellValue("國有土地所有權"); sheet1.AddMergedRegion(new CellRangeAddress(3, 3, 4, 5)); row3.GetCell(4).CellStyle = styleContent; row3.CreateCell(6).SetCellValue("國有土地使用權"); sheet1.AddMergedRegion(new CellRangeAddress(3, 3, 6, 7)); row3.GetCell(6).CellStyle = styleContent; row3.CreateCell(8).SetCellValue("集體土地所有權"); sheet1.AddMergedRegion(new CellRangeAddress(3, 3, 8, 9)); row3.GetCell(8).CellStyle = styleContent; row3.CreateCell(10).SetCellValue("集體土地使用權"); sheet1.AddMergedRegion(new CellRangeAddress(3, 3, 10, 11)); row3.GetCell(10).CellStyle = styleContent; IRow row4 = sheet1.CreateRow(4); row4.CreateCell(2).SetCellValue("登記數量"); row4.CreateCell(3).SetCellValue("宗地面積"); row4.CreateCell(4).SetCellValue("登記數量"); row4.CreateCell(5).SetCellValue("宗地面積"); row4.CreateCell(6).SetCellValue("登記數量"); row4.CreateCell(7).SetCellValue("宗地面積"); row4.CreateCell(8).SetCellValue("登記數量"); row4.CreateCell(9).SetCellValue("宗地面積"); row4.CreateCell(10).SetCellValue("登記數量"); row4.CreateCell(11).SetCellValue("宗地面積"); row4.GetCell(2).CellStyle = styleContent; row4.GetCell(3).CellStyle = styleContent; row4.GetCell(4).CellStyle = styleContent; row4.GetCell(5).CellStyle = styleContent; row4.GetCell(6).CellStyle = styleContent; row4.GetCell(7).CellStyle = styleContent; row4.GetCell(8).CellStyle = styleContent; row4.GetCell(9).CellStyle = styleContent; row4.GetCell(10).CellStyle = styleContent; row4.GetCell(11).CellStyle = styleContent; //(obj[0] as Dictionary<string,object>)["xzqmc"] int rowNumRe = 5; for (int i = 0; i < objArr.Length; i++) { object objDic = objArr[i]; var objDicList = objDic as Dictionary<string, object>; IRow rowInfo = sheet1.CreateRow(rowNumRe); rowNumRe++; string xzqdm=objDicList["xzqdm"].ToString(); string xzqmc=objDicList["xzqmc"].ToString(); string allnum=objDicList["allnum"].ToString(); string allmj=objDicList["allmj"].ToString(); string num10=objDicList["num10"].ToString(); string gytdsuoyq10=objDicList["gytdsuoyq10"].ToString(); string num20=objDicList["num20"].ToString(); string gytdshiyq20=objDicList["gytdshiyq20"].ToString(); string num30=objDicList["num30"].ToString(); string jttdsuoyq30=objDicList["jttdsuoyq30"].ToString(); string num40=objDicList["num40"].ToString(); string jttdshiyq40=objDicList["jttdshiyq40"].ToString(); rowInfo.CreateCell(0).SetCellValue(xzqdm); rowInfo.CreateCell(1).SetCellValue(xzqmc); rowInfo.CreateCell(2).SetCellValue(allnum); rowInfo.CreateCell(3).SetCellValue(allmj); rowInfo.CreateCell(4).SetCellValue(num10); rowInfo.CreateCell(5).SetCellValue(gytdsuoyq10); rowInfo.CreateCell(6).SetCellValue(num20); rowInfo.CreateCell(7).SetCellValue(gytdshiyq20); rowInfo.CreateCell(8).SetCellValue(num30); rowInfo.CreateCell(9).SetCellValue(jttdsuoyq30); rowInfo.CreateCell(10).SetCellValue(num40); rowInfo.CreateCell(11).SetCellValue(jttdshiyq40); rowInfo.GetCell(0).CellStyle = styleContent; rowInfo.GetCell(1).CellStyle = styleContent; rowInfo.GetCell(2).CellStyle = styleContent; rowInfo.GetCell(3).CellStyle = styleContent; rowInfo.GetCell(4).CellStyle = styleContent; rowInfo.GetCell(5).CellStyle = styleContent; rowInfo.GetCell(6).CellStyle = styleContent; rowInfo.GetCell(7).CellStyle = styleContent; rowInfo.GetCell(8).CellStyle = styleContent; rowInfo.GetCell(9).CellStyle = styleContent; rowInfo.GetCell(10).CellStyle = styleContent; rowInfo.GetCell(11).CellStyle = styleContent; } }
(3)匯出的效果圖如下:
上面的設定樣式的方法還可以寫的更全面點,把其封裝起來,當然網上還有不同的處理方式。