2012-08-10 61 views
18

Estoy tratando de abrir un documento de Excel utilizando EPPlus reference/package. No puedo abrir la aplicación Excel. ¿Qué código me estoy perdiendo?Abrir documento de Excel con EPPlus

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

He tratado pck.open(newFile);, pero no lo permite ...

+0

¿Desea abrir la hoja de cálculo en Excel? – 3aw5TZetdf

+0

Sí, eso es lo que intento hacer, solo quiero que se muestre para que pueda empezar a aprender y trabajar con él ... –

+0

bool.xls es una hoja de cálculo Excel simple que se encuentra en mi Escritorio –

Respuesta

27

Prueba esto:

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

Espero que esto ayude!

+0

Gracias perfectos :) –

+0

uso su muestra pero me lanza este error ... System.InvalidOperationException: Ya existe una hoja de cálculo con este nombre en el libro en OfficeOpenXml.ExcelWorksheets.Add (String Name) –

+0

Todavía no funciona con archivos .xlsm –

Cuestiones relacionadas