查询数据集导入到Execl

 List<PrintExcelCell> dicSource//查询出来的数据集

 private static void ExportRptFormExcelTmp(Page contentPage, string templateFullName, List<PrintExcelCell> dicSource)
        {
            Microsoft.Office.Interop.Excel.Application xlsApp = null;
            Microsoft.Office.Interop.Excel.Workbook newBook = null;
            Microsoft.Office.Interop.Excel.Worksheet newSheet = null;
            System.Diagnostics.Process p = null;

            string guid = Guid.NewGuid().ToString();
            try
            {
                //打开Excel
                xlsApp = new Microsoft.Office.Interop.Excel.Application();
                xlsApp.Visible = false;
                xlsApp.AlertBeforeOverwriting = false;
                xlsApp.AskToUpdateLinks = false;
                xlsApp.DisplayAlerts = false;
                //新建空文档
                xlsApp.Workbooks.Add(contentPage.Server.MapPath(string.Format("~\\Print\\template\\{0}", templateFullName)));

                //激活当前工作簿
                newBook = xlsApp.ActiveWorkbook;
                newSheet = (Microsoft.Office.Interop.Excel.Worksheet)newBook.Sheets[1];


                foreach (PrintExcelCell pos in dicSource)
                {
                    Microsoft.Office.Interop.Excel.Range tmpRange = newSheet.Cells[pos.X, pos.Y] as Microsoft.Office.Interop.Excel.Range;
                    tmpRange.Value = pos.CellValue;
                    if (!string.IsNullOrEmpty(pos.CellStyleName))
                        tmpRange.Style = pos.CellStyleName;
                }
                //保存产生的excel
                string savePath = contentPage.Server.MapPath("~/Print/");

                //判断office版本
                string strVer = xlsApp.Version;
                decimal ver = Convert.ToDecimal(strVer);
                if (ver >= 12)
                    newBook.SaveAs(savePath + guid + ".xlsx", Type.Missing, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                else
                    newBook.SaveAs(savePath + guid + ".xls", Type.Missing, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                //打开生成Excel文件
                newBook.Close(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                xlsApp.Quit();
                if (p != null)
                {
                    if (p.ProcessName.ToUpper() == "EXCEL.EXE" && !p.HasExited)
                        p.Kill();
                }
                if (ver >= 12)
                    contentPage.Response.Redirect("~/Print/" + guid + ".xlsx", false);
                else
                    contentPage.Response.Redirect("~/Print/" + guid + ".xls", false);
            }
            catch (Exception ex)
            {
                if (xlsApp != null)
                    xlsApp.Quit();
                throw ex;
            }
        

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值