導出Excel 把一個dataset的多個datatable導入到一個excel的多個sheet中

    /// <summary>
    /// 導出Excel 把一個dataset的多個datatable導入到一個excel的多個sheet中
    /// </summary>
    /// <param name="ds">DataSet</param>
    /// <param name="tableNames">ds裏每個表的表名</param>
    /// <param name="strExcelFileName">導出Excel名稱(YYYY-MM-DD.xls)</param>
    public void doExport(DataSet ds, string[] tableNames, string strExcelFileName)
    {
        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        try
        {
            excel.Visible = false;
            //設置禁止彈出保存和覆蓋的詢問提示框
            excel.DisplayAlerts = false;
            excel.AlertBeforeOverwriting = false;
            //增加一個工作簿
            Workbook book = excel.Workbooks.Add(true);
            //添加工作表
            Worksheet sheets = (Microsoft.Office.Interop.Excel.Worksheet)
                book.Worksheets.Add(Missing.Value, Missing.Value, 19, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);

            for (int i = 0; i < ds.Tables.Count; i++)
            {
                System.Data.DataTable table = ds.Tables[i];
                //獲取一個工作表
                Worksheet sheet = book.Worksheets[i + 1] as Worksheet;
                int rowIndex = 1;
                int colIndex = 0;

                foreach (DataColumn col in table.Columns)
                {
                    colIndex++;
                    sheet.Cells[1, colIndex] = col.ColumnName;
                }
                foreach (DataRow row in table.Rows)
                {
                    rowIndex++;
                    colIndex = 0;
                    foreach (DataColumn col in table.Columns)
                    {
                        colIndex++;
                        //在這裏要在數字前加前單引號
                        String typeName = row[col.ColumnName].GetType().ToString();
                        sheet.Cells[rowIndex, colIndex] = typeCheckAdd(row[col.ColumnName].ToString(), typeName);
                    }
                }
                sheet.Name = tableNames[i];
            }

            //刪除多餘Sheet
            for (int g = 1; g <= book.Worksheets.Count; g++)
            {
                Worksheet sheet = book.Worksheets[g] as Worksheet;
                if (sheet.Name.Substring(0, 5) == "Sheet")
                {
                    sheet.Delete();
                    g--;
                }
            }
            book.Save();
            book.SaveAs(strExcelFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            excel.Quit();
            excel = null;
            GC.Collect();

            SaveXls(strExcelFileName);//保存到選定路徑
        }
        catch(Exception e)
        {
            Response.Write(e.Message);
        }
    }
    #region 若是大數需加前導引號變成字符串
    public String typeCheckAdd(String cellContent, String strType)
    {
        String cellContentAdd;
        switch (strType)
        {
            case "System.String":
                cellContentAdd = "'" + cellContent;
                break;
            default:
                cellContentAdd = cellContent;
                break;
        }
        return cellContentAdd;
    }
    #endregion
    #region 保存已生成Excel到選定路徑
    protected void SaveXls(string fileURL)
    {
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileURL);
        Response.Clear();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(fileInfo.Name.ToString()));
        Response.AddHeader("content-length", fileInfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.Default;
        Response.WriteFile(fileURL);
    }
    #endregion

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章