using
Microsoft.Office.Interop.Owc11;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
SpreadsheetClass xlsheet
=
new
SpreadsheetClass();
//
合并单元格
xlsheet.get_Range(xlsheet.Cells[
1
,
1
],xlsheet.Cells[
1
,
14
]).set_MergeCells(
true
);
xlsheet.ActiveSheet.Cells[
1
,
1
]
=
"
一级帐表
"
;
//
字体加粗
xlsheet.get_Range(xlsheet.Cells[
1
,
1
],xlsheet.Cells[
1
,
14
]).Font.set_Bold(
true
);
//
单元格文本水平居中对齐
xlsheet.get_Range(xlsheet.Cells[
1
,
1
],xlsheet.Cells[
1
,
14
]).set_HorizontalAlignment( XlHAlign.xlHAlignCenter );
//
设置字体大小
xlsheet.get_Range(xlsheet.Cells[
1
,
1
],xlsheet.Cells[
1
,
14
]).Font.set_Size(
14
);
//
设置列宽
xlsheet.get_Range(xlsheet.Cells[
1
,
3
],xlsheet.Cells[
1
,
3
]).set_ColumnWidth(
50
);
//
画边框线
xlsheet.get_Range(xlsheet.Cells[
1
,
1
],xlsheet.Cells[
10
,
15
]).Borders.set_LineStyle( XlLineStyle.xlContinuous );
Random r
=
new
Random( );
//
写入数据 (这里可根据需要由DS生成)
for
(
int
row
=
2
;row
<
10
;row
++
)
//
注意写入数据时,必须从第一行开始写EXCEL里没有第"0"行
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
for(int i = 0 ;i< 15 ;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
xlsheet.ActiveSheet.Cells[row, i + 1]=r.Next(10000,99999) ;
}
}
try
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//将数字格式化为金额(要格式化的单元格内的值必须为数值型)
xlsheet.get_Range(xlsheet.Cells[2,1],xlsheet.Cells[10,15]).set_NumberFormat("¥#,##0.00");
xlsheet.Export("e:\wwwroot\_TestOWC.xls", SheetExportActionEnum.ssExportActionNone, SheetExportFormat.ssExportXMLSpreadsheet);
Response.Write("Export OK");
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
转载: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1485986
转载于:https://www.cnblogs.com/Dragon-China/archive/2007/02/05/640329.html