学习
实践
活动
专区
工具
TVP
写文章

EPPLUS excel新增数据保存失败?

  • 回答 ( 1 )
  • 关注 ( 0 )
  • 查看 ( 751 )

epplus 只有在创建文件时能正确写入excel,打开已有文件时不能正常写入,

using System.Windows.Forms;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Drawing;
using System.Data;
using System;
using System.IO;
using System.Threading;
namespace WindowsFormsApp1
    public class ExcelExport
        //传入一个excel文件路径和一个字符串数组,把数组追加到excel
        public ExcelExport(string filePath, string[] strlist)
            //打开文件
            using (FileStream file = new FileStream(filePath, FileMode.OpenOrCreate))
                ExcelPackage package = new ExcelPackage(file);
                ExcelWorksheet worksheet;
                //判断是新建还是打开
                    worksheet = package.Workbook.Worksheets[1];
                catch
                    worksheet = package.Workbook.Worksheets.Add("Sheet1");
                //如果表是空的,从第一行开始写
                if (worksheet.Dimension == null)
                    for (int i = 0; i < strlist.Length; i++)
                        worksheet.Cells[1, i + 1].Value = strlist[i];
                    //这里能正常保存
                    package.Save();
                    return;
                //如果表里有数据,就写到数据的下一行
                int row = worksheet.Dimension.End.Row;
                for (int i = 0; i < strlist.Length; i++)
                    worksheet.Cells[row + 1, i + 1].Value = strlist[i];
                //这里就不行了
                package.Save();