ASP.NET 使用myxls导出excel

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值