我编写了以下代码来使用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) 我编写了下面的程序,使用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)