您的位置:首页 > 其它

npoi本地文件

2015-08-30 17:09 267 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace ReadExcel
{
class Program
{
static void Main(string[] args)
{
//要操作的excel文件路径
string path = @"F:\zhxl\NPOI\zhxl.xlsx";

//把文件内容导入到工作薄当中,然后关闭文件
FileStream fs = File.OpenRead(path);
IWorkbook workbook = new XSSFWorkbook(fs);
fs.Close();

//编辑工作薄当中内容
ISheet sheet = workbook.GetSheetAt(0);
for (int i = 0; i <= sheet.LastRowNum;i++ )
{
foreach (ICell cell in sheet.GetRow(i).Cells)
{
/*
* Excel数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,就会出现Cannot get a text value from a numeric cell的异常错误。
* 解决办法:先设置Cell的类型,然后就可以把纯数字作为String类型读进来了
*/
cell.SetCellType(CellType.String);
cell.SetCellValue((Int32.Parse(cell.StringCellValue) * 2).ToString());
}
}

//把编辑过后的工作薄重新保存为excel文件
FileStream fs2 = File.Create(@"F:\zhxl\NPOI\zhxl2.xlsx");
workbook.Write(fs2);
fs2.Close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: