NPOI操作Excel,NPOI画较复杂表格和设置其单元格格式

        在最近的项目中,又使用了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)导出的效果图如下:

 

上面的设置样式的方法还可以写的更全面点,把其封装起来,当然网上还有不同的处理方式。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值