XlsDocument xls =
new XlsDocument();
//新建一個xls文檔
xls.FileName =
"MyXlsDemo.xls";
//設定Excel文件名
xls.SummaryInformation.Author =
"Terry Li";
//填加Excel文件做者信息
xls.SummaryInformation.Subject =
"MyXls Demo";
//填加文件主題信息
xls.DocumentSummaryInformation.Company =
"in2bits.org";
//填加文件公司信息
string sheetName =
"第一個Sheet Demo";
#region
string sheetName =
"第一個Sheet Demo";
Worksheet sheet = xls.Workbook.Worksheets.Add(sheetName);
//填加名爲"第一個Sheet Demo"的sheet頁
Cells cells = sheet.Cells;
//Cells實例是sheet頁中單元格(cell)集合
//單元格1-base
Cell cell = cells.Add(2, 3,
"三");
//設定第2行,第3例單元格的值
cell.HorizontalAlignment = HorizontalAlignments.Centered;
//設定文字居中
cell.Font.FontName =
"行楷";
//設定字體
cell.Font.Height = 30 * 20;
//設定字大小(字體大小是以 1/20 point 爲單位的)
cell.UseBorder =
true;
//使用邊框
cell.BottomLineStyle = 2;
//設定邊框底線爲粗線
cell.BottomLineColor = Colors.Red;
//設定顏色爲紅色
cell.RightLineStyle = 2;
cell.RightLineColor = Colors.Red;
//cell的格式還能夠定義在一個xf對象中
XF cellXF = xls.NewXF();
//爲xls生成一個XF實例(XF是cell格式對象)
cellXF.HorizontalAlignment = HorizontalAlignments.Centered;
//設定文字居中
cellXF.Font.FontName =
"隸書";
//設定字體
cellXF.Font.Height = 30 * 20;
//設定字大小(字體大小是以 1/20 point 爲單位的)
cellXF.UseBorder =
true;
//使用邊框
cellXF.BottomLineStyle = 2;
//設定邊框底線爲粗線
cellXF.BottomLineColor = Colors.Green;
//設定顏色爲綠色
cellXF.LeftLineStyle = 2;
//設定邊框左線爲粗線
cellXF.LeftLineColor = Colors.Green;
cell = cells.Add(3, 3,
"國", cellXF);
//以設定好的格式填加cell
cellXF.Font.FontName =
"仿宋_GB2312";
cellXF.BottomLineStyle = 2;
//設定邊框底線爲粗線
cellXF.BottomLineColor = Colors.Blue;
//設定顏色爲藍色
cellXF.RightLineStyle = 2;
//設定邊框右線爲粗線
cellXF.RightLineColor = Colors.Blue;
//設定顏色爲藍色
cellXF.LeftLineStyle = 0;
cell = cells.Add(4, 3,
"志", cellXF);
//格式能夠屢次使用
//ColumnInfo colInfo = new ColumnInfo(xls, sheet);//生成列格式對象
////設定colInfo格式的起做用的列爲第2列到第5列(列格式爲0-base)
//colInfo.ColumnIndexStart = 1;//起始列爲第二列
//colInfo.ColumnIndexEnd = 5;//終止列爲第六列
//colInfo.Width = 15 * 256;//列的寬度計量單位爲 1/256 字符寬
//sheet.AddColumnInfo(colInfo);//把格式附加到sheet頁上(注:AddColumnInfo方法有點小問題,不給把colInfo對象屢次附給sheet頁)
//colInfo.ColumnIndexEnd = 6;//能夠更改列對象的值
//ColumnInfo colInfo2 = new ColumnInfo(xls, sheet);//經過新生成一個列格式對象,纔到能設定其它列寬度
//colInfo2.ColumnIndexStart = 7;
//colInfo2.ColumnIndexEnd = 8;
//colInfo2.Width = 20 * 256;
//sheet.AddColumnInfo(colInfo2);
MergeArea meaA =
new MergeArea(2, 3, 5, 7);
//一個合併單元格實例(合併第2行、第5例 到 第3行、第7例)
sheet.AddMergeArea(meaA);
//填加合併單元格
cellXF.VerticalAlignment = VerticalAlignments.Centered;
cellXF.Font.FontName =
"隸書";
//cellXF.Font.Height = 48 * 20;
//cellXF.Font.Bold = true;
cellXF.Pattern = 1;
//設定單元格填充風格。若是設定爲0,則是純色填充(無色),1表明沒有間隙的實色
cellXF.PatternBackgroundColor = Colors.Red;
//填充的底色
cellXF.PatternColor = Colors.Green;
//設定填充線條的顏色
cell = cells.Add(2, 5,
"晉/陳壽", cellXF);
#endregion
sheet.Cells.Merge(7, 9, 1, 4);
cell = cells.Add(7, 1,
"MyXls 合併單元格 Demo");
cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.VerticalAlignment = VerticalAlignments.Centered;
for (
int sheetNumber = 1; sheetNumber <= 4; sheetNumber++)
{
sheetName =
"Sheet " + sheetNumber;
int rowMin = sheetNumber;
int rowCount = sheetNumber + 10;
int colMin = sheetNumber;
int colCount = sheetNumber + 10;
sheet = xls.Workbook.Worksheets.Add(sheetName);
cells = sheet.Cells;
for (
int r = 0; r < rowCount; r++)
{
if (r == 0)
{
for (
int c = 0; c < colCount; c++)
{
cells.Add(rowMin + r, colMin + c,
"Column" + (c + 1)).Font.Bold =
true;
}
}
else
{
for (
int c = 0; c < colCount; c++)
{
int val = r + c;
cell = cells.Add(rowMin + r, colMin + c, val+
":51CTO五歲了!");
if (val % 2 != 0)
{
cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.Font.FontName =
"Times New Roman";
cell.Font.Underline = UnderlineTypes.Double;
cell.Font.ColorIndex = 2;
cell.Rotation = 45;
//字符傾斜45度
}
}
}
}
}
xls.Send();
//XlsDocument.SendMethods.Inline