zoukankan      html  css  js  c++  java
  • NPOI简单操作excel

    本文仅当是个记录文件,仅供初学者参考。

    首先得using几个npoi的空间名如下:

    using NPOI.HSSF.UserModel;
    using NPOI.HSSF.Util;
    using NPOI.HPSF;
    using NPOI.POIFS.FileSystem;
    using NPOI.SS.UserModel;
    using NPOI.SS.Util;
    using System.IO;

    添加函数的方法如下:

    /// <summary>
            /// excel添加图片的方法
            /// </summary>
            /// <param name="p_sheet">创建的sheet</param>
            /// <param name="p_workbook">workbook</param>
            /// <param name="p_szFileUrl">图片的url</param>
            /// <param name="p_irow"></param>
            /// <param name="p_icol"></param>
            public static void AddPic(ISheet p_sheet,HSSFWorkbook p_workbook,string p_szFileUrl,int p_irow,int p_icol) {
                try {
                    string szFileNm = p_szFileUrl;
                    if (!string.IsNullOrEmpty(szFileNm) && File.Exists(szFileNm)) {
                        byte[] bytes = System.IO.File.ReadAllBytes(szFileNm);
                        int iPictureIdx = 0;
                        iPictureIdx = p_workbook.AddPicture(bytes,PictureType.JPEG);
                        HSSFPatriarch patriarch = (HSSFPatriarch)p_sheet.CreateDrawingPatriarch();
                        HSSFClientAnchor anchor = new NPOI.HSSF.UserModel.HSSFClientAnchor(0, 0, 1023, 0, p_irow, p_icol, p_irow + 2, p_icol+2);
                        HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, iPictureIdx);
                    }
                }
                catch (Exception ex) { }
            }

    这里我添加了一个button按钮,点击后将数据写入excel中

    protected void Button1_Click(object sender, EventArgs e)
            {
    
    //创建工作薄
                HSSFWorkbook wk = new HSSFWorkbook();
                //创建一个名称为mySheet的表,当然你也可以get一个已存在的sheet
                ISheet tb = wk.CreateSheet("mySheet");
    
                //创建一行,此行为第二行
                IRow row = tb.CreateRow(1);
                IRow row1 = tb.CreateRow(3);
                IRow row2 = tb.CreateRow(4);
                ICell cell1 = row1.CreateCell(0);
                ICell cell2 = row1.CreateCell(1);
                ICell cell3 = row2.CreateCell(0);
                ICell cell4 = row2.CreateCell(1);
                string szPicUrl = @"C:\Users\qq00067767\Desktop\xxx.png"; //图片地址
                AddPic(tb, wk, szPicUrl, 1, 1);
    
                for (int i = 0; i < 20; i++)
                {
                    ICell cell = row.CreateCell(i);  //在第二行中创建单元格
                    cell.SetCellValue(i);//循环往第二行的单元格中添加数据
                }
                //using (FileStream fs = File.OpenWrite(@"C:\Users\qq00067767\Desktop\xqqtest.xls"))//打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
                //{
                //    wk.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
    
                //}
                FileStream oFile = new FileStream(@"C:\Users\qq00067767\Desktop\xqqtest.xls", FileMode.Open, FileAccess.ReadWrite);
    
                wk.Write(oFile);
                
                oFile.Close();
    
            }

    经过测试,没有问题!!!

  • 相关阅读:
    BestCoder17 1001.Chessboard(hdu 5100) 解题报告
    codeforces 485A.Factory 解题报告
    codeforces 485B Valuable Resources 解题报告
    BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
    codeforces 374A Inna and Pink Pony 解题报告
    codeforces 483B Friends and Presents 解题报告
    BestCoder15 1002.Instruction(hdu 5083) 解题报告
    codeforces 483C.Diverse Permutation 解题报告
    codeforces 483A. Counterexample 解题报告
    NSArray中地内存管理 理解
  • 原文地址:https://www.cnblogs.com/yuhuabaobao/p/4386127.html
Copyright © 2011-2022 走看看