21

EPPlusリファレンス/パッケージを使用して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);ましたが、それは許可されていません...

4

2 に答える 2

32

これを試して:

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");
}

お役に立てれば!

于 2012-08-10T08:17:44.817 に答える
1

win32例外を回避するには、ASPNETCoreでこれを試してください。

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();
    }
}
于 2019-09-13T05:15:01.620 に答える