Code Bye

用npoi怎样在excel中插入一行

例如说,原本的excel是这样的:

现在本人想在“名单”下面加一行日期

用npoi怎样在excel中插入一行?

用createRow不行。

解决方案:20分
private void WriteExcel(string strPath)

        {

            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;

            }

        }


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用npoi怎样在excel中插入一行