2016-06-27 12 views
4

.aspxページの.xlsダウンロードをc#EppPlusを使用して.xlsxダウンロードにアップグレードしています。代替の行の背景色を追加するにはどうすればいいですか?私はまたどこすることができますLoadFromDataTableためのオーバーロードがあることを言及されるべきだと思う、私は以下のコードコードの部分の下EPP Plus C#.XLSXで代替行フォーマットを追加する方法

public void DumpExcel(DataTable tbl) 
{ 
    using (ExcelPackage pck = new ExcelPackage()) 
    { 
     //Create the worksheet 
     ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1"); 


     ws.Cells["A1"].LoadFromDataTable(tbl, true); 



     using (ExcelRange rng = ws.Cells["A1:AA1"]) 
     { 
      rng.Style.Font.Bold = false; 
      rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid 
      rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(0, 51, 153)); //Set color to dark blue 
      rng.Style.Font.Color.SetColor(Color.White); 
      rng.Style.Font.Size = 10; 
     } 



     // Add Word wrap 
     for (int i = 1; i <= tbl.Columns.Count; i++) 
     { 
      ws.Column(i).AutoFit(); 
      ws.Column(i).Width = 20; 
      ws.Column(i).Style.WrapText = true; 
      ws.Column(i).Style.VerticalAlignment = ExcelVerticalAlignment.Top; 
      ws.Column(i).Style.Font.Size = 9; 
     } 


     //Write it back to the client 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.AddHeader("content-disposition", "attachment; filename=UserEntitleDLX.xlsx"); 
     Response.BinaryWrite(pck.GetAsByteArray()); 
    } 
} 
+0

たいのですか?また、私の答えに注意してくださいそれはそれがテーブルとしてフォーマットされたい場合は、フォーマットする方がはるかに簡単な方法かもしれないので – chandler

答えて

3

のための仕事をしてくれましたそのようにTableStyleを渡す

ws.Cells["A1"].LoadFromDataTable(tbl, true, TableStyles.Dark1); 

tblの領域をscrからフォーマットするにはあなたはこのようなことをすることができます

for (var row = 1; row <= tbl.Rows.Count; row++) 
{ 
    for (var column = 1; column <= tbl.Columns; column++) 
    { 
     ws.Cells[row, column].Style.Font.Bold = false; 
     ws.Cells[row, column].Style.Fill.PatternType = ExcelFillStyle.Solid; 
     ws.Cells[row, column].Style.Font.Size = 10; 

     ws.Cells[row, column].Style.Fill.BackgroundColor.SetColor(column%2 == 0 
      ? Color.Blue 
      : Color.Gray); 
    } 
} 
2

を使用しています

は私

for (int row = ws.Dimension.Start.Row; row <= ws.Dimension.End.Row; row++) 
       { 
        int pos = row % 2; 
        ExcelRow rowRange = ws.Row(row); 
        ExcelFill RowFill = rowRange.Style.Fill; 
        RowFill.PatternType = ExcelFillStyle.Solid; 
        switch (pos) 
        { 
         case 0: 
          RowFill.BackgroundColor.SetColor(System.Drawing.Color.White); 

          break; 
         case 1: 
          RowFill.BackgroundColor.SetColor(System.Drawing.Color.LightGray); 
          break; 

        } 
       } 
+0

あなたはワークシート全体をフォーマットしたかったのですか?また、私の答えに注意してください。なぜなら、それがテーブルとしてフォーマットされたほうがはるかに簡単なフォーマット方法かもしれないからです。 – chandler

+0

@Grasshopperこれは、DataTableを必要としないという点で優れたソリューションです。他のすべてのシナリオの 'List 'なども動くだろう – nam

0

私が(拡張子として)使用する回答のバリエーションです。私の要件では、列のサブセットが、行全体ではなく、単なる2色以上で色付けされている必要がありました。

白/グレーを交互
public static void ApplyBackgroundColorsPerRow(
    this ExcelWorksheet worksheet, 
    int startRow, int startColumn, int endRow, int endColumn, 
    List<System.Drawing.Color> colors) 
{ 
    if (startRow <= endRow) 
    { 
     int numberOfColors = colors.Count; 
     for (int row = startRow; row <= endRow; row++) 
     { 
      using (ExcelRange range = worksheet.Cells[row, startColumn, row, endColumn]) 
      { 
       range.Style.Fill.PatternType = ExcelFillStyle.Solid; 
       range.Style.Fill.BackgroundColor.SetColor(colors[(row - startRow) % numberOfColors]); 
      } 
     } 
    } 
} 

(ライトグレーはグレーの上で、私の推薦である)このようになりますが:あなたは、ワークシート全体がフォーマット

worksheet.ApplyBackgroundColorsPerRow(
    startRow: 1, 
    startColumn: 1, 
    endRow: 20, 
    endColumn: 5, 
    colors: new List<System.Drawing.Color>() 
    { 
     System.Drawing.Color.White, 
     System.Drawing.Color.LightGray 
    } 
); 
関連する問題