zoukankan      html  css  js  c++  java
  • DataTable数据导出Excel 并且下载

    public string Excel(System.Data.DataTable dt)

            {

               //模板的路径

                string strUploadPath = HttpContext.Current.Server.MapPath("../template/");

                //模板的名称

    string strFileName = strUploadPath + "JobTicketTemplate.xlsx";

                FileInfo TemplateFile = new FileInfo(strFileName);

                //目标地址

    string strPath = HttpContext.Current.Server.MapPath("../temp/");

                //文件名称

    string strNewFileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";

                FileInfo newFile = new FileInfo(strPath + strNewFileName);

                int intCount = 2;

                using (OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(newFile, TemplateFile))

                {

    // Worksheets指的是第几个Excel

                    OfficeOpenXml.ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                    if (dt != null && dt.Rows.Count > 0)

                    {

                        for (int i = 0; i < dt.Rows.Count; i++)

                        {

                            worksheet.Cell(i + intCount, 1).Value = dt.Rows[i]["Name"].ToString();

                            worksheet.Cell(i + intCount, 2).Value = dt.Rows[i]["RealName"].ToString();

                            worksheet.Cell(i + intCount, 3).Value = dt.Rows[i]["Company"].ToString();

                            worksheet.Cell(i + intCount, 4).Value = dt.Rows[i]["IdentityCard"].ToString();

                            worksheet.Cell(i + intCount, 5).Value = dt.Rows[i]["Province"].ToString();

                            worksheet.Cell(i + intCount, 6).Value = dt.Rows[i]["Email"].ToString();

                            worksheet.Cell(i + intCount, 7).Value = dt.Rows[i]["Mobile"].ToString();

                        }

                    }

                    package.Save();

                }

     

                return strNewFileName;

            }

  • 相关阅读:
    tcp为什么要三次握手
    TCP/IP协议(一)网络基础知识
    拜占庭将军问题深入探讨
    Block Manager
    Standalone 集群部署
    Spark内存管理
    Checkpoint & cache & persist
    Python——在Python中如何使用Linux的epoll
    网络编程——C10K简述
    网络编程——The C10K Problem(C10K = connection 10 kilo 问题)。k 表示 kilo,即 1000
  • 原文地址:https://www.cnblogs.com/it1042290135/p/5526471.html
Copyright © 2011-2022 走看看