基本信息
源码名称:C#使用NPOI给EXCEL模板填充数据
源码大小:10.53M
文件格式:.rar
开发语言:C#
更新时间:2024-06-07
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
C#使用NPOI给EXCEL模板填充数据

 

public void ExcelReadWrite()

        {
            FileStream newfile;
            FileStream file = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
            string hzm = System.IO.Path.GetExtension(FilePath);
            if (hzm.Equals(".xls"))
            {
                HSSFWorkbook Hwk = new HSSFWorkbook(file);
                ISheet sheet1 = Hwk.GetSheet("Sheet1");       //需要引用命名空间using NPOI.SS.UserModel 。ISheet
                sheet1.GetRow(2).GetCell(0).SetCellValue(NametextBox.Text);
                sheet1.GetRow(2).GetCell(1).SetCellValue(GendercomboBox.Text);
                sheet1.GetRow(2).GetCell(2).SetCellValue(AgecomboBox.Text);
                sheet1.GetRow(2).GetCell(3).SetCellValue(WorkYearstextBox.Text);
                sheet1.GetRow(2).GetCell(4).SetCellValue(dateTimePicker1.Text);
                string newpath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//桌面路径
                newfile = new FileStream(newpath @"\new.xls", FileMode.Create);
                Hwk.Write(newfile);
                newfile.Close();
            }
            else
            {
                XSSFWorkbook Xwk = new XSSFWorkbook(file);
                ISheet sheet1 = Xwk.GetSheet("Sheet1");       
                sheet1.GetRow(2).GetCell(0).SetCellValue(NametextBox.Text);
                sheet1.GetRow(2).GetCell(1).SetCellValue(GendercomboBox.Text);
                sheet1.GetRow(2).GetCell(2).SetCellValue(AgecomboBox.Text);
                sheet1.GetRow(2).GetCell(3).SetCellValue(WorkYearstextBox.Text);
                sheet1.GetRow(2).GetCell(4).SetCellValue(dateTimePicker1.Text);
                string newpath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                newfile = new FileStream(newpath @"\new.xlsx", FileMode.Create);
                Xwk.Write(newfile);
                newfile.Close();
            }
        }