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

ASP.NET 使用myxls导出excel

2013-09-29 10:44 477 查看
using org.in2bits.MyXls;


string sTitleName = "工作成绩报表";

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(sTitleName + ".xls"));//添加头信息。为“文件下载/另存为”指定默认文件名称,对中文文件名进行URL编码解决中文乱码
//Response.AddHeader("Content-Length", "机车油耗效率统计.xls");//添加头文件,指定文件的大小,让浏览器显示文件下载的速度

org.in2bits.MyXls.XlsDocument xls = new XlsDocument();
xls.FileName = sTitleName + ".xls";

Worksheet sheet = xls.Workbook.Worksheets.Add("Sheet1");

XF xfBT = xls.NewXF();
xfBT.HorizontalAlignment = HorizontalAlignments.Centered;
xfBT.VerticalAlignment = VerticalAlignments.Centered;
xfBT.Pattern = 1;
xfBT.PatternColor = Colors.Default30;//自己对Colors.Default30进行了修改,改为:无填充色

xfBT.Font.FontName = "宋体";
xfBT.Font.Height = 12 * 20;
xfBT.Font.Bold = true;

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.FontName = "宋体";
xf.Font.Height = 12 * 20;
//xf.Font.ColorIndex = 1;
//xf.Font.Bold = true;

Cells cells = sheet.Cells;

#region 列头

cells.Merge(1, 1, 1, 14);
for (int i = 1; i < 14; i++)
{
cells.Add(1, i, sTitleName, xfBT);
}
sheet.Rows[1].RowHeight = 30 * 20;

cells.Merge(2, 4, 1, 1);
for (int i = 2; i <= 4; i++)
{
cells.Add(i, 1, "机车号", xf);
}
ColumnInfo colInfo = new ColumnInfo(xls, sheet);
colInfo.ColumnIndexStart = 0;
colInfo.ColumnIndexEnd = 0;
colInfo.Width = (ushort)(4 * 2 * 256);
sheet.AddColumnInfo(colInfo);

cells.Merge(2, 4, 2, 2);
for (int i = 2; i <= 4; i++)
{
cells.Add(i, 2, "班次", xf);
}
colInfo = new ColumnInfo(xls, sheet);
colInfo.ColumnIndexStart = 1;
colInfo.ColumnIndexEnd = 1;
colInfo.Width = (ushort)(4 * 2 * 256);
sheet.AddColumnInfo(colInfo);

cells.Merge(2, 4, 3, 3);
for (int i = 2; i <= 4; i++)
{
cells.Add(i, 3, "司机", xf);
}
colInfo = new ColumnInfo(xls, sheet);
colInfo.ColumnIndexStart = 2;
colInfo.ColumnIndexEnd = 2;
colInfo.Width = (ushort)(4 * 2 * 256);
sheet.AddColumnInfo(colInfo);

#endregion

for (int i = 1; i < 15; i++)
{
cells.Add(5, i, i, xf);
}

int nStartRow = 6;
for (int i = 1; i <= dt.Rows.Count; ++i)
{
for (int j = 1; j <= dt.Columns.Count; ++j)
{
Cell cell = cells.Add(i + nStartRow - 1, j, dt.Rows[i - 1][j - 1].ToString(), xf);
}
}

xls.Send();
Response.Flush();
Response.End();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: