2012-04-02 62 views
20

Tôi sử dụng EPPlus để tạo tệp Excel.Làm thế nào tôi có thể tạo ô đa tầng với thư viện EPPlus cho Excel

Tôi muốn chuyển đổi văn bản HTML (in đậm, in nghiêng, màu phông chữ, tên, tham số kích thước) thành Excel Cell. Tôi cho rằng cần phải tạo ô có nhiều kiểu, như:

văn bản ô là "hello!"
phong cách tôi muốn là:

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

hoặc (phức tạp hơn)

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

tôi biết về thư viện MS OpenXML (nó cho phép tôi làm những gì tôi cần). Điều này là tốt nhưng thư viện phức tạp hơn một chút để thực hiện.

+0

Đã giải quyết !!! Tôi có thể sử dụng điều đó: http://pastebin.com/wJfcgyhV –

+1

bạn có thể viết câu trả lời đó và chấp nhận nó để đánh dấu câu hỏi là đã được giải quyết .. – Aprillion

Trả lời

24

Đã giải quyết! Tôi có thể sử dụng:

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

Vì lý do nào đó, câu trả lời của Anton không hiệu quả đối với tôi. Tôi đã sử dụng:

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

Tôi đã sử dụng phiên bản 3.0.0.2 mới nhất vào lúc này. Vui lòng kiểm tra phiên bản lib của bạn. –

1

tôi đã tạo ra một phương pháp mở rộng cho việc này giúp giảm thiểu các mã một chút:

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

Và để sử dụng nó: var bảng = package.Workbook. 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); 
} 

Không chắc chắn tại sao EPPlus không có thứ gì đó như thế này, hoặc có lẽ họ làm và tôi đã bỏ lỡ nó.

Các vấn đề liên quan