Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用epplus C更新现有工作簿#_C#_Excel_Epplus 4 - Fatal编程技术网

C# 使用epplus C更新现有工作簿#

C# 使用epplus C更新现有工作簿#,c#,excel,epplus-4,C#,Excel,Epplus 4,我正在尝试将新工作表添加到现有工作簿中,代码运行良好,没有任何错误。但更改不会更新到excel文件。 这是我的密码 string path = "C:\\TestFileSave\\ABC.xlsx"; FileInfo filePath = new FileInfo(path); if (File.Exists(path)) { using(ExcelPackage p = new ExcelPackage()) { using(stream = new F

我正在尝试将新工作表添加到现有工作簿中,代码运行良好,没有任何错误。但更改不会更新到excel文件。 这是我的密码

string path = "C:\\TestFileSave\\ABC.xlsx";
FileInfo filePath = new FileInfo(path);
if (File.Exists(path)) 
{
    using(ExcelPackage p = new ExcelPackage()) 
    {
        using(stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite)) 
        {
            p.Load(stream);
            ExcelWorksheet ws = p.Workbook.Worksheets.Add(wsName + wsNumber.ToString());
            ws.Cells[1, 1].Value = wsName;
            ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
            ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
            ws.Cells[1, 1].Style.Font.Bold = true;
            p.Save();
        }
    }
}

对象未绑定到
。唯一的关系是它在调用中复制字节以加载,然后它们分开

您甚至不需要使用
——最好让
自己处理它,如下所示:

var fileinfo = new FileInfo(path);
if (fileinfo.Exists)
{
    using (ExcelPackage p = new ExcelPackage(fileinfo))
    {
        //using (stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
        {
            //p.Load(stream);
            ExcelWorksheet ws = p.Workbook.Worksheets.Add(wsName + wsNumber.ToString());
            ws.Cells[1, 1].Value = wsName;
            ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
            ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
            ws.Cells[1, 1].Style.Font.Bold = true;
            p.Save();
        }

    }

}

在这里,我展示了如何通过在同一个文件中创建一个新的工作表,将数据写入现有的excel文件。要回答您的问题,请尝试使用最后两行File.writealBytes而不是p.Save()


我最初通过使用此命令获得错误代码“写入操作期间发生磁盘错误。(HRESULT中的异常:0x8003001D(STG_E_WriteDefault)),但后来了解到这是因为我要修改的现有Excel文件不完全符合MS Excel格式。我在OpenOffice中将原始excel文件创建为.xls文件,但EPPlus无法读取它。当我在在线excel中重新生成此原始excel文件时,一切正常。

请添加一些文字解释您的答案。不要只是把代码扔进去。
string strfilepath = "C:\\Users\\m\\Desktop\\Employeedata.xlsx";           
using (ExcelPackage p = new ExcelPackage())
{
    using (FileStream stream = new FileStream(strfilepath, FileMode.Open))
    {
        p.Load(stream);
       //deleting worksheet if already present in excel file
        var wk = p.Workbook.Worksheets.SingleOrDefault(x => x.Name == "Hola");
        if (wk != null) { p.Workbook.Worksheets.Delete(wk); }

        p.Workbook.Worksheets.Add("Hola");
        p.Workbook.Worksheets.MoveToEnd("Hola");
        ExcelWorksheet worksheet = p.Workbook.Worksheets[p.Workbook.Worksheets.Count];

        worksheet.InsertRow(5, 2);
        worksheet.Cells["A9"].LoadFromDataTable(dt1, true);
        // Inserting values in the 5th row
        worksheet.Cells["A5"].Value = "12010";
        worksheet.Cells["B5"].Value = "Drill";
        worksheet.Cells["C5"].Value = 20;
        worksheet.Cells["D5"].Value = 8;

        // Inserting values in the 6th row
        worksheet.Cells["A6"].Value = "12011";
        worksheet.Cells["B6"].Value = "Crowbar";
        worksheet.Cells["C6"].Value = 7;
        worksheet.Cells["D6"].Value = 23.48;                  
    }
    //p.Save() ;
    Byte[] bin = p.GetAsByteArray();
    File.WriteAllBytes(@"C:\Users\m\Desktop\Employeedata.xlsx", bin);
}