目前MyXls已經(jīng)實現(xiàn)了單元格(cell)的格式設(shè)置,包括文本顏色、文本大小、字體、單位格邊框、底色、列寬、行高,合并單元格,多個sheet頁等功能。以下是MyXLS組件的一些用法:
1.創(chuàng)建一個Excel文檔:
XlsDocument xls = new XlsDocument();
2.創(chuàng)建一個WorkSheet:
Worksheet ws = xls.Workbook.Worksheets.Add("WorkSheet1");
3.指定列格式:
ColumnInfo colInfo = new ColumnInfo(xls, ws);
colInfo.ColumnIndexStart = ;
colInfo.ColumnIndexEnd = 17;
colInfo.Width = 15 * 256;
ws.AddColumnInfo(colInfo);
列格式必須每次都要重新定義,一個列格式不能重復(fù)使用。
4.指定單元格樣式:
XF xf = xls.NewXF();
xf.HorizontalAlignment = HorizontalAlignments.Centered;
xf.VerticalAlignment = VerticalAlignments.Centered;
xf.Pattern = 1;
xf.PatternColor = Colors.Default30;
xf.UseBorder = true;
xf.TopLineStyle = 1;
xf.TopLineColor = Colors.Black;
xf.BottomLineStyle = 1;
xf.BottomLineColor = Colors.Black;
xf.LeftLineStyle = 1;
xf.LeftLineColor = Colors.Black;
xf.RightLineStyle = 1;
xf.RightLineColor = Colors.Black;
xf.Font.Bold = true;
xf.Font.Height = 11 * 20;
xf.Font.ColorIndex = 1;
5.給單元格賦值:
ws.Cells.Add(2, 3, "金額(萬元)", xf);
6.合并單元格:
7.MyXls合并單元格有個bug,就是合并后只是第一個單元格有樣式,其余的樣式丟失。所以寫了個函數(shù)來合并:
MergeRegion(ref ws, xf, "機(jī)構(gòu)", 1, 1, 2, 1);
public void MergeRegion(ref Worksheet ws, XF xf, string title, int startRow, int startCol, int endRow, int endCol)
{
for (int i = startCol; i <= endCol; i++)
{
for (int j = startRow; j <= endRow; j++)
{
ws.Cells.Add(j, i, title, xf);
}
}
ws.Cells.Merge(startRow, endRow, startCol, endCol);
}
雖然效率不怎么樣,但是對于出Excel報表,還OK。
8.指定單元格格式:
cell.Format = StandardFormats.Decimal_1;
具體更多請參考源代碼的StandardFormats類。
9.保存或者發(fā)送Excel:
xls.Send();
//或者
xls.Save();
MyXls下載地址:http://myxls.in2bits.org/Downloads.ashx
01 | //1.創(chuàng)建一個Excel文檔: |
02 | |
03 | XlsDocument xls = new XlsDocument(); |
04 | |
05 | //2.創(chuàng)建一個WorkSheet: |
06 | |
07 | Worksheet ws = xls.Workbook.Worksheets.Add( "WorkSheet1" ); |
08 | |
09 | //3.指定列格式: |
10 | |
11 | ColumnInfo colInfo = new ColumnInfo(xls, ws); |
12 | colInfo.ColumnIndexStart = 0; |
13 | colInfo.ColumnIndexEnd = 17; |
14 | colInfo.Width = 15 * 256; |
15 | ws.AddColumnInfo(colInfo); |
16 | |
17 | //列格式必須每次都要重新定義,一個列格式不能重復(fù)使用。 |
18 | |
19 | //4.指定單元格樣式: |
20 | |
21 | XF xf = xls.NewXF(); |
22 | xf.HorizontalAlignment = HorizontalAlignments.Centered; |
23 | xf.VerticalAlignment = VerticalAlignments.Centered; |
24 | xf.Pattern = 1; |
25 | xf.PatternColor = Colors.Default30; |
26 | xf.UseBorder = true ; |
27 | xf.TopLineStyle = 1; |
28 | xf.TopLineColor = Colors.Black; |
29 | xf.BottomLineStyle = 1; |
30 | xf.BottomLineColor = Colors.Black; |
31 | xf.LeftLineStyle = 1; |
32 | xf.LeftLineColor = Colors.Black; |
33 | xf.RightLineStyle = 1; |
34 | xf.RightLineColor = Colors.Black; |
35 | xf.Font.Bold = true ; |
36 | xf.Font.Height = 11 * 20; |
37 | xf.Font.ColorIndex = 1; |
38 | |
39 | //5.給單元格賦值: |
40 | |
41 | ws.Cells.Add(2, 3, "金額(萬元)" , xf); |
42 | |
43 | //6.合并單元格: |
44 | ws.Cells.Merge(1, 2, 2, 2); |
45 | //或者 |
46 | ws.AddMergeArea( new MergeArea(1, 2, 1, 1)); |
47 | |
48 | //7.MyXls合并單元格有個bug,就是合并后只是第一個單元格有樣式,其余的樣式丟失。所以寫了個函數(shù)來合并: |
49 | |
50 | MergeRegion( ref ws, xf, "機(jī)構(gòu)" , 1, 1, 2, 1); |
51 | |
52 | public void MergeRegion( ref Worksheet ws, XF xf, string title, int startRow, int startCol, int endRow, int endCol) |
53 | { |
54 | for ( int i = startCol; i <= endCol; i++) |
55 | { |
56 | for ( int j = startRow; j <= endRow; j++) |
57 | { |
58 | ws.Cells.Add(j, i, title, xf); |
59 | } |
60 | } |
61 | ws.Cells.Merge(startRow, endRow, startCol, endCol); |
62 | } |
63 | |
64 | //雖然效率不怎么樣,但是對于出Excel報表,還OK。 |
65 | |
66 | //8.指定單元格格式: |
67 | |
68 | cell.Format = StandardFormats.Decimal_1; |
69 | |
70 | //具體更多請參考源代碼的StandardFormats類。 |
71 | |
72 | //9.保存或者發(fā)送Excel: |
73 | |
74 | xls.Send(); |
75 | //或者 |
76 | xls.Save(); |