2012-04-02 6 views
20

Uso EPPlus para la generación de archivos de Excel.Cómo puedo crear una celda de varios tíxeles con la biblioteca EPPlus para Excel

Quiero decir que necesito convertir texto HTML (negrita, cursiva, color de fuente, nombre, parámetros de tamaño) a Excel Cell. Supongo que necesita crear la celda multiescalada, como:

El texto de celda es "¡Hola!"
el estilo que quiero es:

he - bold 
ll - italic 
o! - red colored font 

o (más compleja)

hello! - bold 
ll - italic (also bold) 
o! - red colored (also bold) 

que sé de la biblioteca MS OpenXML (que me permite hacer lo que necesito). Esta es una biblioteca buena pero un poco más compleja para su implementación.

+0

Resuelto !!! Puedo usar eso: http://pastebin.com/wJfcgyhV –

+1

puedes escribir eso como una respuesta y aceptarlo para marcar la pregunta como resuelta ... – Aprillion

Respuesta

24

¡Resuelto! puedo usar esa:

FileInfo fi = new FileInfo(@"c:\Book1.xlsx"); 

     using (ExcelPackage package = new ExcelPackage(fi)) 
     { 
     // add a new worksheet to the empty workbook 
     ExcelWorksheet worksheet = package.Workbook.Worksheets["Inv"]; 
     //Add the headers 

     worksheet.Cells[2, 1].IsRichText = true; 
     ExcelRichText ert = worksheet.Cells[2, 1].RichText.Add("bugaga"); 
     ert.Bold = true; 
     ert.Color = System.Drawing.Color.Red; 
     ert.Italic = true; 

     ert = worksheet.Cells[2, 1].RichText.Add("alohaaaaa"); 
     ert.Bold = true; 
     ert.Color = System.Drawing.Color.Purple; 
     ert.Italic = true; 

     ert = worksheet.Cells[2, 1].RichText.Add("mm"); 
     ert.Color = System.Drawing.Color.Peru; 
     ert.Italic = false; 
     ert.Bold = false; 


     package.Save(); 
     } 
3

Por alguna razón no funcionó respuesta de Anton para mí. Tuve que usar:

FileInfo fi = new FileInfo(@"c:\Book1.xlsx"); 

using (ExcelPackage package = new ExcelPackage(fi)) 
{ 
    // add a new worksheet to the empty workbook 
    ExcelWorksheet worksheet = package.Workbook.Worksheets["Inv"]; 

    //add multi-coloured text to a cell 
    worksheet.Cells[2, 1].IsRichText = true; 
    ExcelRichTextCollection rtfCollection = worksheet.Cells[2, 1].RichText; 
    ExcelRichText ert = rtfCollection.Add("bugaga"); 
    ert.Bold = true; 
    ert.Color = System.Drawing.Color.Red; 
    ert.Italic = true; 

    ert = rtfCollection.Add("alohaaaaa"); 
    ert.Bold = true; 
    ert.Color = System.Drawing.Color.Purple; 
    ert.Italic = true; 

    ert = rtfCollection.Add("mm"); 
    ert.Color = System.Drawing.Color.Peru; 
    ert.Italic = false; 
    ert.Bold = false; 

    package.Save(); 
} 
+0

He usado la última versión 3.0.0.2 en este momento. Por favor revisa la versión de tu lib. –

1

He creado un método de extensión para este que ayuda a reducir el código un poco:

public static class RichtTextExtensions 
{ 
    public static ExcelRichText Add(this ExcelRichTextCollection richTextCollection, 
     string text, bool bold = false, bool italic = false, Color? color = null, float size = 11, 
     bool underline = false, string fontName = "Segoe UI Light") 
    { 
     var richText = richTextCollection.Add(text); 

     richText.Color = color ?? Color.Black; 
     richText.Bold = bold; 
     richText.Italic = italic; 
     richText.Size = size; 
     richText.FontName = fontName; 
     richText.UnderLine = underline; 

     return richText; 
    } 
} 

Y utilizarlo: var = package.Workbook hoja de trabajo. Worksheets.Add ("Hoja1");

using (ExcelRange cellRange = worksheet.Cells[1,1]) 
{ 
    cellRange.RichText.Add("This is ", size: 18, underline:true); 
    cellRange.RichText.Add("a simple ", bold: true, size: 18, underline: true); 
    cellRange.RichText.Add("test ", size: 18, underline: true); 
    cellRange.RichText.Add("of the extension method", bold: true, size: 18, underline: true); 
} 

No estoy seguro de por qué EPPlus no tiene algo como esto, o tal vez lo hacen y me lo perdí.

Cuestiones relacionadas