2012-04-02 24 views
20

Excel dosya üretimi için EPPlus kullanıyorum.Excel için EPPlus kitaplığı ile çoklu kanallı hücre nasıl oluşturabilirim?

HTML metnini (kalın, italik, yazı tipi rengi, ad, boyut parametreleri) Excel Cell'e dönüştürmem gerekiyor. Sanırım çok stilli bir hücre oluşturması gerekiyor, örneğin:

Hücre metni "merhaba!"
istediğim tarzıdır:

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

ya da (daha karmaşık)

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

I (bana ihtiyacım olanı yapmaya izin verir), MS açık XML kütüphanesinde biliyorum. Bu uygulama için biraz daha karmaşık bir kütüphanedir.

+0

Çözüldü !!! Bunu kullanabilirim: http://pastebin.com/wJfcgyhV –

+1

bunu bir cevap olarak yazabilir ve sorunu çözülmüş olarak kabul etmek için kabul edebilirsiniz .. – Aprillion

cevap

24

Çözüldü! Bunu kullanabilirsiniz:

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

benim için nedense Anton cevabı daha hiç iş için.

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

Ve kullanmak için: var çalışma = package.Workbook

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

Şu anda en son 3.0.0.2 sürümünü kullandım. Lütfen lib sürümünüzü kontrol edin. –

1

Ben kodu bir tad azaltmaya yardımcı Bunun bir uzantısı yöntemi oluşturduk: Ben kullanmak zorunda kaldı. Worksheets.Add ("Sheet1");

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

EPPlus zaten böyle bir şey yoktur, ya da belki yaptıkları ve bunu cevapsız neden emin değilim.