using Microsoft.Office.Interop.Owc11;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.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"行
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
for(int i = 0 ;i< 15 ;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
xlsheet.ActiveSheet.Cells[row, i + 1]=r.Next(10000,99999) ;
}
}
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.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
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
转载: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1485986