欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Npoi操作excel的解决办法

程序员文章站 2023-12-12 15:38:28
npoi 简介-------------------------------------------------------------------------------...

npoi 简介
--------------------------------------------------------------------------------

1.整个excel表格叫做工作表:workbook(工作薄),包含的叫页(工作表):sheet;行:row;单元格cell。

2.npoi 下载地址:http://npoi.codeplex.com/releases/view/38113

4.忘了告诉大家npoi是做什么的了,npoi 能够读写几乎所有的office 97-2003文件格式,至少能够支持word, powerpoint, excel, visio的格式。

--------------------------------------------------------------------------------

使用npoi创建一个简单的xls文件

--------------------------------------------------------------------------------

复制代码 代码如下:

//创建xls文件
        private void button1_click(object sender, eventargs e)
        {
            //创建工作薄
            hssfworkbook wk = new hssfworkbook();
            //创建一个名称为mysheet的表
            isheet tb = wk.createsheet("mysheet");
            //创建一行,此行为第二行
            irow row = tb.createrow(1);
            for (int i = 0; i < 20; i++)   
            {
                icell cell = row.createcell(i);  //在第二行中创建单元格
                cell.setcellvalue(i);//循环往第二行的单元格中添加数据
            }
            using (filestream fs = file.openwrite(@"c:/myxls.xls")) //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
            {
                wk.write(fs);   //向打开的这个xls文件中写入mysheet表并保存。
                messagebox.show("提示:创建成功!");
            }
        }

使用npoi读取一个简单的xls文件

--------------------------------------------------------------------------------
复制代码 代码如下:

//读取xls文件
        private void button2_click(object sender, eventargs e)
        {   stringbuilder sbr = new stringbuilder();
            using (filestream fs = file.openread(@"c:/myxls.xls"))   //打开myxls.xls文件
            {
                hssfworkbook wk = new hssfworkbook(fs);   //把xls文件中的数据写入wk中
                for (int i = 0; i < wk.numberofsheets; i++)  //numberofsheets是myxls.xls中总共的表数
                {
                    isheet sheet = wk.getsheetat(i);   //读取当前表数据
                    for (int j = 0; j <= sheet.lastrownum; j++)  //lastrownum 是当前表的总行数
                    {
                        irow row = sheet.getrow(j);  //读取当前行数据
                        if (row != null)
                        {
                            sbr.append("-------------------------------------\r\n"); //读取行与行之间的提示界限
                            for (int k = 0; k <= row.lastcellnum; k++)  //lastcellnum 是当前行的总列数
                            {
                                icell cell = row.getcell(k);  //当前表格
                                if (cell != null)
                                {                                  
                                    sbr.append(cell.tostring());   //获取表格中的数据并转换为字符串类型
                                }
                            }
                        }
                    }
                }              
            }
            sbr.tostring();
            using (streamwriter wr = new streamwriter(new filestream(@"c:/mytext.txt", filemode.append)))  //把读取xls文件的数据写入mytext.txt文件中
            {
                wr.write(sbr.tostring());
                wr.flush();
            }

        }

--------------------------------------------------------------------------------

使用npoi创建一个常用的xls文件

--------------------------------------------------------------------------------

复制代码 代码如下:

//创建一个常用的xls文件
        private void button3_click(object sender, eventargs e)
        {         
            iworkbook wb = new hssfworkbook();
            //创建表 
            isheet sh = wb.createsheet("zhiyuan");
            //设置单元的宽度 
            sh.setcolumnwidth(0, 15 * 256);
            sh.setcolumnwidth(1, 35 * 256);
            sh.setcolumnwidth(2, 15 * 256);
            sh.setcolumnwidth(3, 10 * 256);
            int i = 0;
            #region 练习合并单元格
            sh.addmergedregion(new npoi.ss.util.cellrangeaddress(0, 0, 0, 3));

            //cellrangeaddress()该方法的参数次序是:开始行号,结束行号,开始列号,结束列号。

            irow row0 = sh.createrow(0);
            row0.height = 20 * 20;
            icell icell1top0 = row0.createcell(0);
            icell1top0.cellstyle = getcellstyle(wb, stylexls.头);
            icell1top0.setcellvalue("标题合并单元");
            #endregion
            i++;
            #region 设置表头
            irow row1 = sh.createrow(1);
            row1.height = 20 * 20;

            icell icell1top = row1.createcell(0);
            icell1top.cellstyle = getcellstyle(wb, stylexls.头);
            icell1top.setcellvalue("网站名");

            icell icell2top = row1.createcell(1);
            icell2top.cellstyle = getcellstyle(wb, stylexls.头);
            icell2top.setcellvalue("网址");

            icell icell3top = row1.createcell(2);
            icell3top.cellstyle = getcellstyle(wb, stylexls.头);
            icell3top.setcellvalue("百度快照");

            icell icell4top = row1.createcell(3);
            icell4top.cellstyle = getcellstyle(wb, stylexls.头);
            icell4top.setcellvalue("百度收录");
            #endregion 

            using(filestream stm=file.openwrite(@"c:/mymergecell.xls"))
            {
                wb.write(stm);
                messagebox.show("提示:创建成功!");
            }
        }

 

        #region 定义单元格常用到样式的枚举
        public enum stylexls
        {
            头,
            url,
            时间,
            数字,
            钱,
            百分比,
            中文大写,
            科学计数法,
            默认
        }
        #endregion


        #region 定义单元格常用到样式
        static icellstyle getcellstyle(iworkbook wb, stylexls str)
        {
            icellstyle cellstyle = wb.createcellstyle();

            //定义几种字体 
            //也可以一种字体,写一些公共属性,然后在下面需要时加特殊的 
            ifont font12 = wb.createfont();
            font12.fontheightinpoints = 10;
            font12.fontname = "微软雅黑";


            ifont font = wb.createfont();
            font.fontname = "微软雅黑";
            //font.underline = 1;下划线 


            ifont fontcolorblue = wb.createfont();
            fontcolorblue.color = hssfcolor.olive_green.blue.index;
            fontcolorblue.isitalic = true;//下划线 
            fontcolorblue.fontname = "微软雅黑";


            //边框 
            cellstyle.borderbottom = npoi.ss.usermodel.borderstyle.dotted;
            cellstyle.borderleft = npoi.ss.usermodel.borderstyle.hair;
            cellstyle.borderright = npoi.ss.usermodel.borderstyle.hair;
            cellstyle.bordertop = npoi.ss.usermodel.borderstyle.dotted;
            //边框颜色 
            cellstyle.bottombordercolor = hssfcolor.olive_green.blue.index;
            cellstyle.topbordercolor = hssfcolor.olive_green.blue.index;

            //背景图形,我没有用到过。感觉很丑 
            //cellstyle.fillbackgroundcolor = hssfcolor.olive_green.blue.index; 
            //cellstyle.fillforegroundcolor = hssfcolor.olive_green.blue.index; 
            cellstyle.fillforegroundcolor = hssfcolor.white.index;
            // cellstyle.fillpattern = fillpatterntype.no_fill; 
            cellstyle.fillbackgroundcolor = hssfcolor.blue.index;

            //水平对齐 
            cellstyle.alignment = npoi.ss.usermodel.horizontalalignment.left;

            //垂直对齐 
            cellstyle.verticalalignment = verticalalignment.center;

            //自动换行 
            cellstyle.wraptext = true;

            //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对 
            cellstyle.indention = 0;

            //上面基本都是设共公的设置 
            //下面列出了常用的字段类型 
            switch (str)
            {
                case stylexls.头:
                    // cellstyle.fillpattern = fillpatterntype.least_dots; 
                    cellstyle.setfont(font12);
                    break;
                case stylexls.时间:
                    idataformat datastyle = wb.createdataformat();

                    cellstyle.dataformat = datastyle.getformat("yyyy/mm/dd");
                    cellstyle.setfont(font);
                    break;
                case stylexls.数字:
                    cellstyle.dataformat = hssfdataformat.getbuiltinformat("0.00");
                    cellstyle.setfont(font);
                    break;
                case stylexls.钱:
                    idataformat format = wb.createdataformat();
                    cellstyle.dataformat = format.getformat("¥#,##0");
                    cellstyle.setfont(font);
                    break;
                case stylexls.url:
                    fontcolorblue.underline = 1;
                    cellstyle.setfont(fontcolorblue);
                    break;
                case stylexls.百分比:
                    cellstyle.dataformat = hssfdataformat.getbuiltinformat("0.00%");
                    cellstyle.setfont(font);
                    break;
                case stylexls.中文大写:
                    idataformat format1 = wb.createdataformat();
                    cellstyle.dataformat = format1.getformat("[dbnum2][$-804]0");
                    cellstyle.setfont(font);
                    break;
                case stylexls.科学计数法:
                    cellstyle.dataformat = hssfdataformat.getbuiltinformat("0.00e+00");
                    cellstyle.setfont(font);
                    break;
                case stylexls.默认:
                    cellstyle.setfont(font);
                    break;
            }
            return cellstyle;


        }
        #endregion


--------------------------------------------------------------------------------
提示:1.以上使用npoi版本为1.2.5版本,版本目前属于最高版本,跟以前版本的使用是有些差别的。

      2.使用以上代码,需要添加两个npoi的dll。ionic.zip.dll,npoi.dll

上一篇:

下一篇: