您的位置:首页 > 编程语言 > C#

C#生成Excel报表 用MyXls组件生成更完美

2014-06-08 21:07 211 查看
protected void Page_Load(object sender, EventArgs e)

private void xlsGridview(DataSet ds, string xlsName)
{
XlsDocument xls = new XlsDocument();

int rowIndex = 1;
int colIndex = 0;

System.Data.DataTable table = ds.Tables[0];
Worksheet sheet = xls.Workbook.Worksheets.AddNamed("sheet");//状态栏标题名称
Cells cells = sheet.Cells;
foreach (DataColumn col in table.Columns)
{
colIndex++;
//sheet.Cells.AddValueCell(1,colIndex,col.ColumnName);//添加XLS标题行
cells.AddValueCell(1, colIndex,col.ColumnName);
}

foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
//sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里
Cell cell= cells.AddValueCell(rowIndex, colIndex, Convert.ToDouble(row[col.ColumnName].ToString()));//转换为数字型
//如果你数据库里的数据都是数字的话 最好转换一下,不然导入到Excel里是以字符串形式显示。
cell.Font.FontFamily = FontFamilies.Roman; //字体
cell.Font.Bold = true; //字体为粗体
}
}
xls.Send();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: