npoi 指定单元格赋值

.Net技术 码拜 9年前 (2015-02-12) 3561次浏览 0个评论

using (FileStream fs = File.OpenRead(@”C:\Users\Administrator\Desktop/myxls.xls”))   //打开myxls.xls文件
{
StringBuilder sbr = new StringBuilder();
HSSFWorkbook wk = new HSSFWorkbook(fs);   //把xls文件中的数据写入wk中
for (int i = 0; i < wk.NumberOfSheets; i++)  //NumberOfSheets是myxls.xls中总共的表数
{
ISheet sheet = wk.GetSheetAt(i);   //读取当前表数据
for (int j = 1; j <= sheet.LastRowNum; j++)  //LastRowNum 是当前表的总行数
{
IRow row = sheet.GetRow(j);  //读取当前行数据
if (row != null)
{
CradNo = row.Cells[16].ToString().Substring(0, 6);
sheet.GetRow(j).GetCell(17).SetCellValue(“中国银行”);
}
}
}
}
sheet.GetRow(j).GetCell(17).SetCellValue(“中国银行”); 这行代码报没有实例化。
我需要循环的在某列的单元格中赋值。怎么弄 下。求教

npoi 指定单元格赋值
100分
 sheet.GetRow(j).GetCell(17).SetCellValue(“中国银行”);
==》row.createCell(17).SetCellValue(new HSSFRichTextString(“中国银行”));
这样试试行不行。
if (row != null)
{
CradNo = row.Cells[16].ToString().Substring(0, 6);
row.createCell(17).SetCellValue(new HSSFRichTextString(“中国银行”));
}
else
{
row = sheet.createRow(j);
row.createCell(17).SetCellValue(new HSSFRichTextString(“中国银行”));
}
npoi 指定单元格赋值
   if (row != null)
{
CradNo = row.Cells[16].ToString().Substring(0, 6);
ICell cell = sheet.CreateRow(j).CreateCell(17);
sheet.GetRow(j).GetCell(17).SetCellValue(“中国银行”);

}

ICell cell = sheet.CreateRow(j).CreateCell(17);  增加了一行这个代码,没报错了。程序调试也正常。
但是完了之后,excel没有增加进去。。。 请问怎么回事?


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明npoi 指定单元格赋值
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!