惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - speshow

初涉SQL Server性能问题(4/4):列出最耗资源的会话 初涉SQL Server性能问题(3/4):列出阻塞的会话 初涉SQL Server性能问题(2/4):列出等待资源的会话 初涉SQL Server性能问题(1/4):服务器概况 分享 大型网站的架构设计问题--大型高并发高负载网站的系统架构 Android应用程序运行机制解析 js 小小jquery等比例缩放图片效果 - speshow - 博客园 .net 发送手机短信息 C#实现汉字转换为拼音缩写的代码 IIs6+Rewrite+asp+php5+mysql5 .net 生成二级域名 用VSTS进行网站压力测试 各种JAVASCRIPT技巧 使用ASP.NET画饼状图 - speshow - 博客园 我们到底为了什么钻研技术? 生成图片水印 aspnetpager 分页 Jquery 服务器回调为select填值
.net 导出海量数据到execl文件 - speshow - 博客园
speshow · 2010-07-16 · via 博客园 - speshow

  对于导出Execl文件大量时 一般做法时间会很长, 下面个人收藏方法,可导海量数据

  protected void CreateExecl(string swhere,string title) {
    string saveFileName = Server.MapPath("http://www.cnblogs.com/uploads/file/" + title);
    bool fileSaved = false;

    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    if (xlApp == null) {
      return;
    }

    Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
    Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
    System.Data.DataTable table = new System.Data.DataTable();
    table.Columns.Add("tagno", typeof(string));
    table.Columns.Add("AddDate", typeof(string));
    SqlDataReader dr = mydb.ExecuteReaderSQL("SELECT tagno,AddDate FROM tagno WHERE " + swhere + " ORDER BY tagnoid ASC", null);
    while (dr.Read()) {
      table.Rows.Add(dr["tagno"].ToString(), dr["AddDate"].ToString());
    }
    dr.Close();

    long rows = table.Rows.Count;
    Int32 _pagecount = 60000;
    if (rows > 60000) {
      long pageRows = _pagecount;//定义每页显示的行数,行数必须小于
      int scount = (int)(rows / pageRows);
      if (scount * pageRows < table.Rows.Count)//当总行数不被pageRows整除时,经过四舍五入可能页数不准
          {
        scount = scount + 1;
      }
      for (int sc = 1; sc <= scount; sc++) {
        if (sc > 1) {
          object missing = System.Reflection.Missing.Value;
          worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(missing, missing, missing, missing);//添加一个sheet
        }
        else {
          worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[sc];//取得sheet1
        }
        string[,] datas = new string[pageRows + 1, 2];
        datas[0, 0] = "吊牌号";
        datas[0, 1] = "生成时间";


        Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 2]);
        range.Interior.ColorIndex = 15;//15代表灰色
        range.Font.Bold = true;
        range.Font.Size = 9;
        int init = int.Parse(((sc - 1) * pageRows).ToString());
        int r = 0;
        int index = 0;
        Int32 result;
        if (pageRows * sc >= table.Rows.Count) {
          result = table.Rows.Count;
        }
        else {
          result = int.Parse((pageRows * sc).ToString());
        }

        for (r = init; r < result; r++) {
          index = index + 1;
          for (int i = 0; i < 2; i++) {
            object obj = table.Rows[r][i];
            datas[index, i] = obj == null ? "" : "'" + obj.ToString().Trim();
          }
        }
        Microsoft.Office.Interop.Excel.Range fchR = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[index +1, 2]);
        fchR.Value2 = datas;
        worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
        range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[index + 1, 2]);

        //15代表灰色
        range.Font.Size = 9;
        range.RowHeight = 14.25;
        range.Borders.LineStyle = 1;
        range.HorizontalAlignment = 1;
      }
    }
    else {
      string[,] datas = new string[table.Rows.Count + 2, 2];
      datas[0, 0] = "吊牌号";
      datas[0, 1] = "生成时间";

      Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 2]);
      range.Interior.ColorIndex = 15;//15代表灰色
      range.Font.Bold = true;
      range.Font.Size = 9;

      int r = 0;
      for (r = 0; r < table.Rows.Count; r++) {
        for (int i = 0; i < 2; i++) {
          //if (gridview.GetVisibleColumn(i).ColumnType == typeof(string) || gridview.GetVisibleColumn(i).ColumnType == typeof(Decimal) || gridview.GetVisibleColumn(i).ColumnType == typeof(DateTime))
          //{
          object obj = table.Rows[r][i];
          datas[r + 1, i] = obj == null ? "" : "'" + obj.ToString().Trim();
          //}
        }
      }
      Microsoft.Office.Interop.Excel.Range fchR = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[table.Rows.Count + 2, 2]);

      fchR.Value2 = datas;
      worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
      range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[table.Rows.Count + 1, 2]);

      //15代表灰色
      range.Font.Size = 9;
      range.RowHeight = 14.25;
      range.Borders.LineStyle = 1;
      range.HorizontalAlignment = 1;
    }

    if (saveFileName != "") {
      //try
      //{
      workbook.Saved = true;
      workbook.SaveCopyAs(saveFileName);
      fileSaved = true;
      //}
      //catch (Exception ex)
      //{
      //    fileSaved = false;
      //}
    }
    else {
      fileSaved = false;
    }

    xlApp.Quit();
    GC.Collect();//强行销毁
    if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
  }