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

推荐订阅源

Scott Helme
Scott Helme
N
Netflix TechBlog - Medium
AI
AI
Security Latest
Security Latest
GbyAI
GbyAI
P
Proofpoint News Feed
Y
Y Combinator Blog
A
Arctic Wolf
G
Google Developers Blog
U
Unit 42
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
WordPress大学
WordPress大学
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
The Hacker News
The Hacker News
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
Project Zero
Project Zero
博客园_首页

博客园 - chinachen

查看DLL 版本 平台 x86 x64 MS SQL2008 数据库备份(收集) android 使用sqlite几种方式。 C# 接口 索引 属性 实质及应用 (收集) get set 用法总结 (收集) MVC 资料收集(一) windows运行命令大全 Html:meta标签的奥妙 (收集) - chinachen - 博客园 ASP.NET抓取页面并分析页面数据的研究与探讨。 C#中indexof和substring函数用法 (收集) - chinachen - 博客园 BlogEngine 深入研究(一) 开篇 ASP.NET实现回调服务器方法 (收集) 统一过程,敏捷过程,微软过程(收集) HTTP服务器状态代码定义(Status Code Definitions)(收集) Lambda 表达式 源自MSDN C# 参考之方法参数关键字:params、ref及out (收集) nchar,char,varchar 与nvarchar区别 (收集) - chinachen 抽象类与接口区别 (收集) virtual 与 Abstract 区别
多个表 导出EXCEL 代码 (分享)
chinachen · 2010-02-25 · via 博客园 - chinachen

多个表 导出EXCEL 

 public void ExportExcel(DataSet dsExcel)
        {

            DataSet ds 

= dsExcel;
            
if (ds == nullreturn;string saveFileName = "";
            
bool fileSaved = false;
            SaveFileDialog saveDialog 
= new SaveFileDialog();
            saveDialog.DefaultExt 
= "xls";
            saveDialog.Filter 
= "Excel文件|*.xls";
            saveDialog.FileName 
= "招生数据备份";
            saveDialog.ShowDialog();
            saveFileName 
= saveDialog.FileName;
            
if (saveFileName.IndexOf(":"< 0return//被点了取消 

            Excel.Application xlApp 
= new Excel.Application();if (xlApp == null)
            {
                MessageBox.Show(
"无法创建Excel对象,可能您的机子未安装Excel");
                
return;
            }

            Excel.Workbooks workbooks 

= xlApp.Workbooks;
            
string[] tableName ="TB_Dic_ExamArea","TB_Dic_ExamArea_SpecialityKinds","TB_Dic_ExamRoom","TB_Dic_SpecialityFocus","TB_ExamAchievement","TB_SignUpStudents","TB_SignUpStudentsFamilyMember","TB_SignUpStudentsPayFee","TB_SignUpStudentsResume","TB_SignUpStudentsSpeciality","TB_SystemInfo","TB_Users"};

            Excel.Workbook workbook 

= workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            
for (int a = 0; a < 12; a++)
            {
                Excel.Worksheet newWorksheet;
                newWorksheet 
= (Excel.Worksheet)workbook.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                newWorksheet.Name 
= tableName[11-a];
            }
            
//
            proValue = 0;
            
for (int b = 0; b < 12; b++)
            {

                Excel.Worksheet worksheet 

= (Excel.Worksheet)workbook.Worksheets[b+1];//取得sheet1 
                
//写入字段 
                int cellNum = ds.Tables[b].Columns.Count;
                
for (int i = 0; i < cellNum; i++)
                {
                    worksheet.Cells[
1, i + 1= ds.Tables[b].Columns[i].ColumnName;

                    worksheet.get_Range(worksheet.Cells[

11 + i], worksheet.Cells[1 + ds.Tables[b].Rows.Count, 1 + i]).NumberFormatLocal = "@";
                }
                
//写入数值 

                
int rowNum = ds.Tables[b].Rows.Count;
                
for (int r = 0; r < rowNum; r++)
                {
                    
for (int i = 0; i < cellNum; i++)
                    {
                        worksheet.Cells[r 
+ 2, i + 1= ds.Tables[b].Rows[r][i].ToString();
                    }
                    System.Windows.Forms.Application.DoEvents();
                }
                Excel.Range rg 
= worksheet.get_Range(worksheet.Cells[22], worksheet.Cells[ds.Tables[b].Rows.Count + 12]);
                rg.NumberFormat 
= "00000000";
                proValue
++;
                progressBar1.PerformStep();
            }
            
if (saveFileName != "")
            {
                
try
                {
                    workbook.Saved 
= true;
                    workbook.SaveCopyAs(saveFileName);
                    fileSaved 
= true;
                }
                
catch (Exception ex)
                {
                    fileSaved 
= false;
                    MessageBox.Show(
"导出文件时出错,文件可能正被打开!\n" + ex.Message);
                }
            }
            
else
            {
                fileSaved 
= false;
            }
            xlApp.Quit();
            GC.Collect();
//强行销毁 
            if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
            progressBar1.Value = 0;
        }

 代码