当前位置: 移动技术网 > IT编程>开发语言>.net > Excel的创建和读取NPOI

Excel的创建和读取NPOI

2019年06月22日  | 移动技术网IT编程  | 我要评论

9oal玉足网,三月男婴身体无故自燃医生无解,吴雨婵透视

一个.net库,可以读取/写入没有安装microsoft office的office格式。没有com +,没有互操作。

public class excelnpoitest
{
    /// <summary>
    /// npoi基础的excel创建
    /// </summary>
    public static void createexcel()
    {
        //创建工作薄
        hssfworkbook wk = new hssfworkbook();
        //创建一个名称为mysheet的表
        isheet tb = wk.createsheet("mysheet");
        for (int i = 0; i < 20; i++)
        {
            //创建行
            irow row = tb.createrow(i);
            for (int j = 0; j < 20; j++)
            {
                //创建列
                icell cell = row.createcell(j);
                //赋值
                cell.setcellvalue($"[{i},{j}]");
            }
        }
        //打开一个现有文件或创建一个新文件以进行写入。
        using (filestream fs = file.openwrite(@"d:\myxls.xls"))
        {
            //向打开的这个xls文件中写入mysheet表并保存。
            wk.write(fs);
        }
    }

    /// <summary>
    /// npoi基础的excel读取
    /// </summary>
    public static void readexcel()
    {
        //打开现有文件以进行读取。
        using (filestream fs = file.openread(@"d:\myxls.xls"))
        {
            //把文件写入wk中
            hssfworkbook wk = new hssfworkbook(fs);
            for (int i = 0; i < wk.numberofsheets; i++)
            {
                //获取sheet
                isheet sheet = wk.getsheetat(i);
                for (int j = 0; j < sheet.lastrownum; j++)
                {
                    //获取行
                    irow row = sheet.getrow(j);
                    if (row != null)
                    {
                        for (int k = 0; k < row.lastcellnum; k++)
                        {
                            //获取列
                            icell cell = row.getcell(k);
                            if (cell != null)
                            {
                                //获取值
                                console.write(cell.tostring());
                            }
                        }
                    }
                    console.writeline();
                }
            }
        }
    }


}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网