利用NPOI修改Excel文件
private void button6_Click(object sender, EventArgs e)
{
//string FileName = @"D:\Excel2007-1019.xlsx";
string FileName = @"D:\Excel2003-1102.xls";
if (!File.Exists(FileName))
{
return;
}
if (
(FileName.ToUpper().IndexOf(".xlsx".ToUpper()) < 0)
&& (FileName.ToUpper().IndexOf(".xls".ToUpper()) < 0)
)
{
return;
}
IWorkbook wk = null;
using (FileStream fs = File.Open(FileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
{
if (FileName.ToUpper().IndexOf(".xlsx".ToUpper()) > 0)
{
wk = new XSSFWorkbook(fs);
}
else
{
wk = new HSSFWorkbook(fs);
}
}
ISheet sheet = wk.GetSheetAt(0);
if (wk.GetSheetAt(0) == null)
{
sheet = wk.CreateSheet();
}
// 在现有行的下面添加一些数据
int startRow = sheet.LastRowNum + 1;
IRow row = sheet.CreateRow(startRow);
row.CreateCell(0).SetCellValue("创建时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
row.CreateCell(1).SetCellValue("中");
row.CreateCell(2).SetCellValue("国");
row.CreateCell(3).SetCellValue("南");
row.CreateCell(4).SetCellValue("海");
using (FileStream fileStream = File.Open(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
wk.Write(fileStream);
}
}
posted on 2016-07-21 15:50 LongHuaiYu 阅读(2047) 评论(0) 编辑 收藏 举报