public string CreateExcel(string SelectedBizType, string strReportDate, DropDownList ddlYQ, DropDownList ddlDataTable, ref string strFilePath)
        {
            Application m_objExcel = new Application();
            int pid;
            GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out pid);
            Workbooks m_objBooks = (Workbooks)m_objExcel.Workbooks;
            string strTepPath = string.Empty;
            string strTemplate = string.Empty;
            if (SelectedBizType == "3a448698-b695-4a5a-b405-df18f44da86f")
            {
                strTepPath = ConfigurationManager.AppSettings["TRQBBTemplate"];
                strTemplate = "天然气报表.xls";
            }
            else if (SelectedBizType == "969c388f-b0e2-4571-9876-4312b6a74a54")
            {
                strTepPath = ConfigurationManager.AppSettings["YCBBTemplate"];
                strTemplate = "油藏报表.xls";
            }
            try
            {
                string strFileDir = strTepPath + strReportDate + @"/";//将导出的excel保存到该路径下
                if (System.IO.Directory.Exists(strFileDir))//用来解决产能下载不稳定的问题
                {
                    System.IO.Directory.Delete(strFileDir, true);//如果由于上一次导出未成功,而留下了痕迹,先删除此文件夹,再创建
                }
                System.IO.Directory.CreateDirectory(strFileDir);//创建导出文件夹
                IDataBase oDB = DBFactory.GetDBInstance();

string strYQName = string.Empty;
                string strTemplateChild = string.Empty;
                //遍历油区
                for (int i = 0; i < ddlYQ.Items.Count; i++)
                {
                    strYQName = ddlYQ.Items[i].Text;
                    //遍历表
                    for (int j = 0; j < ddlDataTable.Items.Count; j++)
                    {
                        string strShortCode = string.Empty;
                        string strDtName = ddlDataTable.Items[j].Text;
                      strTemplateChild = "XXXXX.xls";
                     strShortCode = "XXXXXXXXXXXX";
                                             //查询业务表数据
                        string strSql = string.Format("select * from " + strShortCode + " where ny='{0}' and yq='{1}'", strReportDate.Substring(0, 6), strYQName);
                        System.Data.DataTable dtLast = oDB.GetDataTable(strSql);
                        if (dtLast.Rows.Count == 0)
                        {
                            continue;
                        }
                        _Workbook m_objBook = (_Workbook)m_objBooks.Add(strTepPath + strTemplateChild);
                        Sheets m_objSheets = (Sheets)m_objBook.Worksheets;
                        _Worksheet m_objSheet = (_Worksheet)(m_objSheets.get_Item(1));
                        //查询表结构字段
                        //                        string strSchama = string.Format(@"select column_name EnKey from user_tab_columns
                        //                                                                                            where upper(table_name)=upper('{0}')
                        //                                                                                            order by COLUMN_ID", strShortCode);
                        //                        System.Data.DataTable dtCols = oDB.GetDataTable(strSchama);
                        //                        string colNames = string.Empty;
                        //                        for (int k = 0; k < dtCols.Rows.Count; k++)
                        //                        {
                        //                            colNames += dtCols.Rows[k][0].ToString() + ",";
                        //                        }
                        //                        if (colNames.Length > 0)
                        //                        {
                        //                            colNames = colNames.Substring(0, colNames.Length - 1);
                        //                        }
                        //                        DownloadMutiRefineTable ddR = new DownloadMutiRefineTable();
                        //                        System.Data.DataTable dtResource = ddR.GetTableSchema(dtLast, colNames);
                        System.Data.DataTable dtResource = dtLast.Copy();
                        //将dataTable中的数据写入sheet
                        int headRows = m_objSheet.UsedRange.Rows.Count;
                        for (int k = 0; k < dtResource.Rows.Count; k++)
                        {
                            for (int m = 0; m < dtResource.Columns.Count; m++)
                            {
                                if (dtResource.Columns[m].ColumnName.ToUpper() == "STATUS")
                                {
                                    continue;
                                }
                                m_objSheet.Cells[k + headRows + 1, m + 1] = dtResource.Rows[k][m].ToString();
                            }
                        }
                        Range rCells = m_objSheet.Range[m_objSheet.Cells[headRows, 1],
                            m_objSheet.Cells[m_objSheet.UsedRange.Rows.Count, m_objSheet.UsedRange.Columns.Count]];  //shiying20140326 用来解决产能下载不稳定的问题
                        rCells.Borders.LineStyle = XlLineStyle.xlContinuous;
                        string save_path = strFileDir + ddlYQ.Items[i].Text + "_" + strReportDate + "_" + strTemplateChild;
                        m_objBook.SaveAs(save_path, XlFileFormat.xlXMLSpreadsheet, null, null, false, false,
                            XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                        m_objBook.Close(false, null, null);
                    }
                }
                m_objBooks.Close();
                m_objExcel.Application.Quit();
                m_objExcel.Quit();

//将得到的excel打包
                Crc32 crc = new Crc32();
                strFilePath = strTepPath + strReportDate + strTemplate.Replace(".xls", ".zip");
                ZipOutputStream zos = new ZipOutputStream(File.Create(strFilePath));
                //zos.SetLevel(6);
                DirectoryInfo di = new DirectoryInfo(strFileDir);
                foreach (FileInfo item in di.GetFiles())
                {
                    FileStream fs = File.OpenRead(item.FullName);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    ZipEntry entry = new ZipEntry(strReportDate + strTemplate.Substring(0, strTemplate.Length - 4) + @"/" + item.Name);
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    zos.PutNextEntry(entry);
                    zos.Write(buffer, 0, buffer.Length);
                }
                zos.Finish();
                zos.Close();
                System.IO.Directory.Delete(strFileDir, true);//删除导出文件夹             
            }
            catch (Exception err)
            {
                Message = err.Message;
            }
            return Message;
        }

excel 下载的更多相关文章

  1. Flex Excel下载

    最近做Flex里的Excel下载,用as3xls进行Excel导出后,Excel修改编辑后老出现:不能以当前格式保存...若要保存所做的更改,请单击“确定”,然后将其另存为最新的格式. 最后通过JAV ...

  2. DevExpress中GridView Excel下载

    DevExpress中GridView提供了许多Excel下载的方法,如gridView.ExportToExcelOld(sfdExcelDown.FileName); 在修改Bug时,遇到这样问题 ...

  3. ASP.NET Excel下载方法一览

    方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 using System; using System.Co ...

  4. JS JQuery 操作: Json转 Excel 下载文件

    方法的调用 var json = '[' + '{"申请流水号":"123456","保险公司":"测试数据",&quo ...

  5. 自己挖的坑自己填--jxl进行Excel下载堆内存溢出问题

    今天在进行使用 jxl 进行 Excel 下载时,由于数据量大(4万多条接近5万条数据的下载),数据结构过于负责,存在大量大对象(虽然在对象每次用完都设置为null,但还是存在内存溢出问题),加上本地 ...

  6. PHP Excel 下载数据,并分页下载

    直接上代码: 调用下载Excel: $total=$duoduo->count(MOD.' as a',$where); $objExcel= SelfExcelObject(); //导出 i ...

  7. SpringMVC生成Excel下载

    SpringMVC controller里的方法: @RequestMapping(value="/notify/download",produces = {"appli ...

  8. C# MVC 自定义ActionResult实现EXCEL下载

    前言 在WEB中,经常要使用到将数据转换成EXCEL,并进行下载.这里整理资料并封装了一个自定义ActionResult类,便于使用.如果文章对你有帮助,请点个赞. 话不多少,这里转换EXCEL使用的 ...

  9. 将页面内容转为Excel下载

    使用:method1(table); 说明:参数table为table元素的ID; var idTmr; function getExplorer() { var explorer = window. ...

随机推荐

  1. 64位Windows 7平台安装32位Timesten,配置ODBC数据源

    问题: 由于系统版本原因,客户机只能安装32位的Timesten,但客户机的平台是64位的win 7,安装完成后按照常规的控制面板->管理工具->数据源(ODBC)打开的ODBC数据源管理 ...

  2. 织梦DEDECMS网站首页如何实现分页翻页

    织梦DEDECMS模板网站首页如何实现首页分页和翻页 方法如下:(三种方法,自己选择一种来实现分页吧) 第一种:调用ajax和参数的(不推荐)1.必须在DEDE首页模板中的<head>&l ...

  3. My SQL 常用函数

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...

  4. PS 色彩的色相谱

    7- 色彩的色相谱 在这个环中,位于180度夹角的两种颜色(也就是圆的某条直径两端的颜色),称为反转色,又称为互补色.互补的两种颜色之间是此消彼长的关系,小框往蓝色移动的同时就会远离黄色, 黄色=白色 ...

  5. Flask学习记录之Flask-Migrate

    一.配置Flask-Migrate from flask.ext.migrate import Migrate, MigrateCommand migrate = Migrate(app,db) #第 ...

  6. python学习第十一天 -- 函数式编程

    在介绍函数式编程之前,先介绍几个概念性的东西. 什么是函数式编程? 函数式编程的特点: 1.把计算视为函数而非指令; 2.纯函数式编程:不需要变量,没有副作用,测试简单; 3.支持高阶函数,代码简洁. ...

  7. Spark学习笔记--概念知识

    RDD被视为由不同的数据块组成,对于RDD的存取是以数据块为单位的,本质上分区(partition)和数据块(block)是等价的,只是看待的角度不同. 数据块 Spark存储管理模块中所管理的几种主 ...

  8. Debian/Ubuntu 安装bcm43142无线网卡驱动

    Drivers for Broadcom BCM43142 wireless card of Ubuntu/Debian 64-bit Linux 1.Check the wireless card ...

  9. JSP(二)

    一.pageContext对象    1>代表当前JSP页面的运行环境, [作用域仅仅局限于当前JSP页面中,出了该JSP页面, 原PageContext域对象被销毁]    2>封装了对 ...

  10. Codeforces 163A Substring and Subsequence

    http://codeforces.com/problemset/problem/163/A?mobile=true 题目大意:给出两个串,问a的连续子串和b的子串(可以不连续)相同的个数. 思路:在 ...