现在本人想在“名单”下面加一行日期
用npoi怎样在excel中插入一行?
用createRow不行。
{
ISheet sheet = null;
NPOI.SS.UserModel.IWorkbook workbook = null;
IRow row = null;
FileStream fs = null;
try
{
fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
if (strPath.IndexOf(“.xlsx”) > 0) // 2007版本
{
workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
}
else if (strPath.IndexOf(“.xls”) > 0) // 2003版本
{
workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(fs);
}
if (workbook == null)
{
return;
}
sheet = workbook.GetSheetAt(0);
row = sheet.CopyRow(0, 1);
row.CreateCell(0).SetCellValue(DateTime.Today.ToString(“yyyy-MM-dd”));
MemoryStream stream = new MemoryStream();
workbook.Write(stream);
var buf = stream.ToArray();
fs = new FileStream(strPath, FileMode.Create, FileAccess.Write);
fs.Write(buf, 0, buf.Length);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
{
fs.Flush();
fs.Dispose();
fs.Close();
}
row = null;
sheet = null;
workbook = null;
}
}