小编man*_*tha的帖子

尝试使用NPOI编辑现有Excel文件的单元格值

我编写了以下代码来使用C#和NPOI库编辑Excel文件.没有错误,但是在我打开文件后运行代码后,不会编辑单元格的值.我究竟做错了什么?

namespace Project37
{
    class Class1
    {
        public static void Main()
        {
            string pathSource = @"C:\Users\mvmurthy\Downloads\VOExportTemplate.xlsx";

            FileStream fs = new FileStream(pathSource, FileMode.Open, FileAccess.ReadWrite); 
            HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
            HSSFSheet sheet = (HSSFSheet)templateWorkbook.GetSheet("ImportTemplate");
            HSSFRow dataRow = (HSSFRow)sheet.GetRow(4);

            dataRow.GetCell(1).SetCellValue("foo");

            MemoryStream ms = new MemoryStream();
            templateWorkbook.Write(ms);
        }     
    }
}
Run Code Online (Sandbox Code Playgroud)

c# excel npoi

2
推荐指数
1
解决办法
3541
查看次数

C#程序使用NPOI编辑excel(.xls)的单元格值不起作用

我编写了下面的程序,使用NPOI编辑excel文件(.xls)的单元格值,程序运行时没有错误或异常但是值没有得到更新

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.IO;
 using System.Web;
 using NPOI.XSSF.UserModel;
 using NPOI.XSSF.Model;
 using NPOI.HSSF.UserModel;
 using NPOI.HSSF.Model;
 using NPOI.SS.UserModel;
 using NPOI.SS.Util;
 namespace Project37
 {
    class Class1
    {
        public static void Main()
        {
            string pathSource = @"C:\Users\mvmurthy\Desktop\abcd.xls";
            FileStream fs = new FileStream(pathSource, FileMode.Open, FileAccess.ReadWrite); 
            HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
            HSSFSheet sheet = (HSSFSheet)templateWorkbook.GetSheet("Contents");
            HSSFRow dataRow = (HSSFRow)sheet.GetRow(4);
            dataRow.Cells[2].SetCellValue("foo");
            MemoryStream ms = new MemoryStream();
            templateWorkbook.Write(ms);
            ms.Close();
        }     
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# excel npoi

1
推荐指数
1
解决办法
3003
查看次数

标签 统计

c# ×2

excel ×2

npoi ×2

.net ×1