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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
月光博客
月光博客
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
博客园_首页
GbyAI
GbyAI
The Cloudflare Blog
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
量子位
博客园 - Franky
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
J
Java Code Geeks
腾讯CDC
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
Lohrmann on Cybersecurity
S
Securelist
F
Full Disclosure
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
The GitHub Blog
The GitHub Blog

博客园 - 无名

sql2005分页SQL 数据库设计原则 sql2005排名函数 如何提高数据库性能,减少数据库服务器压力瓶颈一两个小方法 sql Server 索引优化 Using DeepLoad In NetTiers 看nettiers整理出来的codesmith模板编写要点 - 无名 - 博客园 .Net WinForm学习笔记 在javascript中,怎样响应键盘事件(转 - 无名 - 博客园 各分数段人数统计 排名问题 DataGrid - 导出Excel文件 DataGrid导出excel和word的例子 DataGrid - 导出Excel文件 DataGrid相关知识总结(收集自各位老大处) 使用DES加密解密代码(C# & vb.Net),好东东,拷得,贴一下 dataGrid的手写样式 在DNN中获取所有模块信息 几个DNN里的技巧(转)(真的是好东东啊)
利用剪切板将DataSet的数据导出到Excel
无名 · 2006-06-09 · via 博客园 - 无名

/// <summary>
  /// 将DataSet里所有数据导入Excel.
  /// 需要添加COM: Microsoft Excel Object Library.
  /// using Excel;
  /// </summary>
  /// <param name="filePath"></param>
  /// <param name="ds"></param>
  public static void ExportToExcel(string filePath, DataSet ds)
  {
   object oMissing = System.Reflection.Missing.Value;
   Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
   try
   {
    // 打开Excel文件。以下为Office 2000.
    Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing,
     oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
     oMissing);
    Excel.Worksheet xlWorksheet;
    // 循环所有DataTable
    for( int i=0; i<ds.Tables.Count; i++ )
    {
     // 添加入一个新的Sheet页。
     xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing);
     // 以TableName作为新加的Sheet页名。
     xlWorksheet.Name = ds.Tables[i].TableName;
     // 取出这个DataTable中的所有值,暂存于stringBuffer中。
     string stringBuffer = "";
     for( int j=0; j<ds.Tables[i].Rows.Count; j++ )
     {
      for( int k=0; k<ds.Tables[i].Columns.Count; k++ )
      {
      
       stringBuffer += ds.Tables[i].Rows[j][k].ToString();
       if( k < ds.Tables[i].Columns.Count - 1 )
        stringBuffer += "\t";
      }
      stringBuffer += "\n";
     }
     // 利用系统剪切板
     System.Windows.Forms.Clipboard.SetDataObject("");
     // 将stringBuffer放入剪切板。
     System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);
     // 选中这个sheet页中的第一个单元格
     ((Excel.Range)xlWorksheet.Cells[1,1]).Select();
     // 粘贴!
     xlWorksheet.Paste(oMissing,oMissing);
     // 清空系统剪切板。
     System.Windows.Forms.Clipboard.SetDataObject("");
    }
    // 保存并关闭这个工作簿。
    xlWorkbook.Close( Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing );
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
    xlWorkbook = null;
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   finally
   {
    // 释放...
    xlApp.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
    xlApp = null;
    GC.Collect();
   }
  }
拷贝来的代码,但是测试过通过
利用操作系统的剪贴板来实现保存
缺点是不好控制excel格式,控制格式怎么实现,没做出来,后来使用copmentOne来做了对excel的操作