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

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - Angelo Dell'inferno

[ASP.NET] 基于.Net Remoting 的网站访问监控模块 [转]Run a Http Response Filter together with an Ajax Update Panel [ASP.NET] HyperLink + Image 实现动态图片链接 [ASP.NET] (转)地址栏参数的判断 [ASP.NET] 实现Label自动换行 [ASP.NET] 验证码生成 [ASP.NET] 实现客户端浏览服务端目录的页面 [ASP.NET]Treeview 控件显示服务端目录文件夹及文件 [C#] GridView导出到Excel [C#] 获取两个时间点的时间间隔 [SQL Server][转]数据库并发控制——活锁&死锁 [JavaScript] 防止页面被嵌入Iframe [SQL Server] 存储过程事务 [SQL Server] T-SQL 连接数据库方法 [C#] 杀Excel进程 [ASP.NET] (原创)自定义GridView分页 [ASP.NET] 服务器端下载文件实现 [Oracle] 日期相关操作 [ORACLE] 函数大全
[C#] 将DataSet内容导入到Excel (矩阵区域导出)
Angelo Dell'inferno · 2007-12-20 · via 博客园 - Angelo Dell'inferno

    /// <summary>
    /// 将DataSet内容导入到Excel
    /// </summary>
    /// <param name="dsExport">DataSet</param>
    public static void FACTDataSetToExcel(System.Data.DataSet dsExport,string _fileName)
    {
        //保存文件名
        string FILE_NAME = ""+_fileName+".xls";
        //数据DataTable
        System.Data.DataTable dt = dsExport.Tables[0];
        string _cellStr = string.Empty;
        //列名
        string strColumnName = string.Empty;
        ApplicationClass xlApp = new ApplicationClass();   
        Workbook wbs = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Worksheet worksheet = (Worksheet)wbs.Worksheets[1];
        #region 导出Excel保存至服务器目录
        if(dt.Rows.Count >0)
        {
            // 设置列名
            for(int head = 0;head<dt.Columns.Count;head++)
            {               
                worksheet.Cells[1,head+1] = dt.Columns[head].ColumnName.ToString();;
                Range rg = (Range)worksheet.Cells[1,head+1];
            }
            try
            {
                object[,] obj = new object[dt.Rows.Count,dt.Columns.Count];
                for(int k=0;k<dt.Rows.Count;k++)
                {
                    for(int l=0;l<dt.Columns.Count;l++)
                    {
                        if (dt.Columns[l].ColumnName.Equals("NET PRICE") || dt.Columns[l].ColumnName.Equals("TRACKING NUMBER")|| dt.Columns[l].ColumnName.Equals("SALES ORDER NO.")|| dt.Columns[l].ColumnName.Equals("DELIVERY DATE")|| dt.Columns[l].ColumnName.Equals("EAN/UPC NUMBER"))
                        {
                            obj[k,l] = "\'"+ dt.Rows[k][l].ToString();
                        }
                        else
                        {
                            obj[k,l] = dt.Rows[k][l].ToString();
                        }
                    }
                }
                Range rangeObj = worksheet.get_Range("A2",Missing.Value);
                rangeObj = rangeObj.get_Resize(dt.Rows.Count,dt.Columns.Count);
                rangeObj.set_Value(Missing.Value,obj);
                wbs.Saved = true;
                wbs.SaveCopyAs(HttpContext.Current.Server.MapPath(".")+"\\FactReports\\"+FILE_NAME);
            }
            #region 异常
            catch
            {
                if (xlApp != null)
                {
                    xlApp.Workbooks.Close();

                    xlApp.Quit();

                    int generation = System.GC.GetGeneration(xlApp);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

                    xlApp = null;
                    System.GC.Collect(generation);
                }

                GC.Collect();//强行销毁

                KillProcess();
            }
                #endregion
            finally
            {
                if (xlApp != null)
                {
                    xlApp.Workbooks.Close();

                    xlApp.Quit();

                    int generation = System.GC.GetGeneration(xlApp);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

                    xlApp = null;
                    System.GC.Collect(generation);
                }

                GC.Collect();//强行销毁

                KillProcess();
            }               
        }
        #endregion
        else
        {
            if (xlApp != null)
            {
                xlApp.Workbooks.Close();

                xlApp.Quit();

                int generation = System.GC.GetGeneration(xlApp);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

                xlApp = null;
                System.GC.Collect(generation);
            }

            GC.Collect();//强行销毁

            KillProcess();
        }
    }

    /// <summary>
    /// 强行杀Excel进程
    /// </summary>
    private static void KillProcess()
    {
        System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
        System.DateTime startTime = new DateTime();
        int m, killId = 0;
        for (m = 0; m < excelProc.Length; m++)
        {
            if (startTime < excelProc[m].StartTime)
            {
                startTime = excelProc[m].StartTime;
                killId = m;
            }
        }
        if (excelProc[killId].HasExited == false)
        {
            excelProc[killId].Kill();
        }
    }