首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用EPPlus打开Excel文档

使用EPPlus打开Excel文档
EN

Stack Overflow用户
提问于 2012-08-10 16:06:41
回答 2查看 62.1K关注 0票数 21

我正在尝试使用EPPlus reference/package打开Excel文档。我无法打开Excel应用程序。我遗漏了什么代码?

protected void BtnTest_Click(object sender, EventArgs e)
{
     FileInfo newFile = new FileInfo("C:\\Users\\Scott.Atkinson\\Desktop\\Book.xls");

     ExcelPackage pck = new ExcelPackage(newFile);
     //Add the Content sheet
     var ws = pck.Workbook.Worksheets.Add("Content");
     ws.View.ShowGridLines = false;

     ws.Column(4).OutlineLevel = 1;
     ws.Column(4).Collapsed = true;
     ws.Column(5).OutlineLevel = 1;
     ws.Column(5).Collapsed = true;
     ws.OutLineSummaryRight = true;

     //Headers
     ws.Cells["B1"].Value = "Name";
     ws.Cells["C1"].Value = "Size";
     ws.Cells["D1"].Value = "Created";
     ws.Cells["E1"].Value = "Last modified";
     ws.Cells["B1:E1"].Style.Font.Bold = true;
}

我试过pck.open(newFile);,但它不允许...

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-10 16:17:45

试试这个:

protected void BtnTest_Click(object sender, EventArgs e)
{
    FileInfo newFile = new FileInfo("C:\\Users\\Scott.Atkinson\\Desktop\\Book.xls");

    ExcelPackage pck = new ExcelPackage(newFile);
    //Add the Content sheet
    var ws = pck.Workbook.Worksheets.Add("Content");
    ws.View.ShowGridLines = false;

    ws.Column(4).OutlineLevel = 1;
    ws.Column(4).Collapsed = true;
    ws.Column(5).OutlineLevel = 1;
    ws.Column(5).Collapsed = true;
    ws.OutLineSummaryRight = true;

    //Headers
    ws.Cells["B1"].Value = "Name";
    ws.Cells["C1"].Value = "Size";
    ws.Cells["D1"].Value = "Created";
    ws.Cells["E1"].Value = "Last modified";
    ws.Cells["B1:E1"].Style.Font.Bold = true;

    pck.Save();
    System.Diagnostics.Process.Start("C:\\Users\\Scott.Atkinson\\Desktop\\Book.xls");
}

希望这能有所帮助!

票数 32
EN

Stack Overflow用户

发布于 2019-09-13 13:15:02

在ASP中尝试此操作,以避免win32异常:

private void CreateWorkbook(string fileName)
{
    using (var p = new ExcelPackage())
    {
        p.Workbook.Properties.Author = "Barry Guvenkaya";
        p.Workbook.Properties.Title = "MyTitle";
        p.Workbook.Worksheets.Add("MySheet");
        var bin = p.GetAsByteArray();
        File.WriteAllBytes(fileName, bin);

        // Below code opens the excel doc
        var proc = new Process();
        proc.StartInfo = new ProcessStartInfo(fileName)
        {
            UseShellExecute = true
        };
        proc.Start();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11897697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档