C# 导出exl

/// <summary>
    /// 导出exl
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PFBtnExport_Click1(object sender, EventArgs e)
    {
        System.Data.DataTable mainInfoTable = null;

        string viewName = "视图名称";

        mainInfoTable = ViewUtil.QueryView(viewName, null, null);

        if (mainInfoTable.Rows.Count == 0)
        {

            MsgBox("没有数据导出!");
            return;
        }       

        else
        {            
              string expFilePath = "xxxx.xls"; //导出文件

             string tempFilePath ="D:\\BimsExportFiles\\xxx.xls"; //临时模版文件路径  

           if (!Directory.Exists("D:\\ExportFiles"))
                Directory.CreateDirectory("d:\\ExportFiles");
                    
            string result = ExportDataIntoExcel(mainInfoTable, 24, "xxxx.xls", expFilePath, tempFilePath);
        }
    }

    //将给定表中前columnNum列的数据导出到Excel文件中
    public string ExportDataIntoExcel(System.Data.DataTable mainInfoTable, int columnNum, string sourcePath, string destinationPath, string tempFilePath)
    {
        string filepath =Server.MapPath(sourcePath);//模板文件路径      

        FileAttributes attrs;
        if (File.Exists(destinationPath))
        {
            //  先把文件的属性读取出来
            attrs = File.GetAttributes(destinationPath);
            //  下面表达式中的 1 是 FileAttributes.ReadOnly 的值
            // 此表达式是把 ReadOnly 所在的位改成 0,
            attrs = (FileAttributes)((int)attrs & ~(1));
            File.SetAttributes(destinationPath, attrs);

            File.Delete(destinationPath);
        }
        if (File.Exists(tempFilePath))
            File.Delete(tempFilePath);
        tempFilePath=Request.MapPath(tempFilePath);
        File.Copy(filepath,tempFilePath,true);

        //  先把文件的属性读取出来
        attrs = File.GetAttributes(tempFilePath);
        //  下面表达式中的 1 是 FileAttributes.ReadOnly 的值
        // 此表达式是把 ReadOnly 所在的位改成 0,
        attrs = (FileAttributes)((int)attrs & ~(1));
        File.SetAttributes(tempFilePath, attrs);

        object missing = Type.Missing;
        Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
        Workbook workbook = application.Workbooks.Open(tempFilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        try
        {
            Worksheet oSheet = (Worksheet)workbook.Sheets.get_Item(1);
            Range oCells = oSheet.Cells;

            for (int row = 0; row < mainInfoTable.Rows.Count; row++)
            {
                //将table数据写入Excel
                int cellRow = row + 2;
                oCells[cellRow, 1] = mainInfoTable.Rows[row][1];

                 oCells[cellRow, 1] = mainInfoTable.Rows[row][2];

               //.......

            }
            //保存
            workbook.SaveAs(tempFilePath, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        finally
        {
            //关闭
            workbook.Close(missing, missing, missing);
            application.Workbooks.Close();
            application.Quit();

            // objBook.Close(false, mode.FullName, missing);

            //结束进程
            if (application != null)
            {
                foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
                {
                    //先判断当前进程是否是excel  
                    if (!p.CloseMainWindow())
                    {
                        p.Kill();
                    }
                }
            }
            workbook = null;
            application = null;
            //强制对所有代进行垃圾回收
            GC.Collect();

        }
        return "OK";

    }

posted on 2012-08-08 10:32  zmmlczy  阅读(288)  评论(0编辑  收藏  举报

导航