ASP.NET DataSet数据生成Excel文档导出下载

1 篇文章 0 订阅
 public partial class am_ElectronicInvoinceExportExcel : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ExcelFile excelFile = new ExcelFile();
            excelFile.Worksheets.Add("发票信息表");
            ExcelWorksheet xlsheet = excelFile.Worksheets[0];
            if (excelFile == null || xlsheet == null)
            {
                base.SetException("无法创建Excel对象,可能您的计算机未安装Excel。请先安装Excel");
                return;
            }

            DataSet ds = new DataSet();
            ds.Merge((DataSet)Session[WebConst.DOWNLOADDATA]);
            //Session[WebConst.DOWNLOADDATA] = null;
            if (OperateUI.HaveData(ds))
            {
                #region 列名
                int rowIndex = 0;
                xlsheet.Rows[rowIndex].Height = 400;
                xlsheet.Rows[rowIndex].Style.Font.Name = "宋体";
                xlsheet.Rows[rowIndex].Style.Font.Size = 9 * 20;
                xlsheet.Rows[rowIndex].Style.VerticalAlignment = VerticalAlignmentStyle.Center;
                xlsheet.Rows[rowIndex].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;

                CellRange cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 0, rowIndex, 0);
                cellRange.Merged = true;
                cellRange.Value = "发票代码";

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 1, rowIndex, 1);
                cellRange.Merged = true;
                cellRange.Value = "发票号码";

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 2, rowIndex, 2);
                cellRange.Merged = true;
                cellRange.Value = "提单号";

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 3, rowIndex, 3);
                cellRange.Merged = true;
                cellRange.Value = "进出口";

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 4, rowIndex, 4);
                cellRange.Merged = true;
                cellRange.Value = "币别";

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 5, rowIndex, 5);
                cellRange.Merged = true;
                cellRange.Value = "金额";

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 6, rowIndex, 6);
                cellRange.Merged = true;
                cellRange.Value = "人民币金额";

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 7, rowIndex, 7);
                cellRange.Merged = true;
                cellRange.Value = "开票日期";
                #endregion

                #region 明细数据
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    rowIndex++;
                    xlsheet.Rows[rowIndex].Height = 400;
                    xlsheet.Rows[rowIndex].Style.Font.Name = "宋体";
                    xlsheet.Rows[rowIndex].Style.Font.Size = 9 * 20;
                    xlsheet.Rows[rowIndex].Style.VerticalAlignment = VerticalAlignmentStyle.Center;
                    xlsheet.Rows[rowIndex].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 0, rowIndex, 0);
                    cellRange.Merged = true;
                    cellRange.Value = dr["ARIVCD"].ToString().Trim();

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 1, rowIndex, 1);
                    cellRange.Merged = true;
                    cellRange.Value = dr["ARIVNO"].ToString().Trim();

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 2, rowIndex, 2);
                    cellRange.Merged = true;
                    cellRange.Value = dr["INBLNO"].ToString().Trim();

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 3, rowIndex, 3);
                    cellRange.Merged = true;
                    cellRange.Value = dr["IHIE"].ToString().Trim();

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 4, rowIndex, 4);
                    cellRange.Merged = true;
                    cellRange.Value = dr["INCURR"].ToString().Trim();

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 5, rowIndex, 5);
                    cellRange.Merged = true;
                    cellRange.Value = dr["INAMT"].ToString().Trim();

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 6, rowIndex, 6);
                    cellRange.Merged = true;
                    cellRange.Value = dr["INCNY"].ToString().Trim();

                    cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 7, rowIndex, 7);
                    cellRange.Merged = true;
                    cellRange.Value = dr["INDATE"].ToString().Trim();
                }
                #endregion

                #region 总额统计
                rowIndex++;
                xlsheet.Rows[rowIndex].Height = 400;
                xlsheet.Rows[rowIndex].Style.Font.Name = "宋体";
                xlsheet.Rows[rowIndex].Style.Font.Size = 9 * 20;
                xlsheet.Rows[rowIndex].Style.VerticalAlignment = VerticalAlignmentStyle.Center;
                xlsheet.Rows[rowIndex].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;

                decimal ipamt = 0;
                decimal ipcny = 0;
                try
                {
                    ipamt = Decimal.Parse(ds.Tables[0].Compute("SUM(INAMT)", "").ToString());
                }
                catch { }
                try
                {
                    ipcny = Decimal.Parse(ds.Tables[0].Compute("SUM(INCNY)", "").ToString());
                }
                catch { }

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 5, rowIndex, 5);
                cellRange.Merged = true;
                cellRange.Value = string.Format("{0:N2}", ipamt);

                cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 6, rowIndex, 6);
                cellRange.Merged = true;
                cellRange.Value = string.Format("{0:N2}", ipcny);
                #endregion

                #region 文件导出
                string strFileName = Path.Combine(Request.MapPath(".") + "\\report\\", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls");
                excelFile.SaveXls(strFileName);

                Response.ClearContent();
                Response.ClearHeaders();
                Response.BufferOutput = true;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls");
                Response.WriteFile(strFileName);
                Response.Flush();

                try
                {
                    System.IO.File.Delete(strFileName);
                }
                catch { }
                #endregion
            }
            else
            {
                base.SetException("浏览器缓存数据出现异常,请重启浏览器后重新操作");
                return;
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值