2012-12-05 141 views
0

我有一個文本文件是在製表符分隔符和以下是我的代碼來生成其Excel。無法生成.xlsx文件

protected void to_excel(object sender, EventArgs e) 
    { 
     string filepath = Path.Combine(Server.MapPath("~/Files"), fileupload.FileName); 
     fileupload.SaveAs(filepath); 
     string fname = fileupload.PostedFile.FileName; 
     DataTable dt = (DataTable)ReadToEnd(filepath); 
     string sFilename = fname.Substring(0, fname.IndexOf(".")); 
     HttpResponse response = HttpContext.Current.Response; 
     Response.Clear(); 
     Response.AddHeader("content-disposition", "attachment;filename=" + sFilename + ".xls"); 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); 
     System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid(); 
     dg.DataSource = dt; 
     dg.DataBind(); 
     dg.RenderControl(htmlWrite); 
     Response.Write(stringWrite.ToString()); 
     Response.End(); 
    } 
    private object ReadToEnd(string filePath) 
    { 
     DataTable dtDataSource = new DataTable(); 
     string[] fileContent = File.ReadAllLines(filePath); 
     if (fileContent.Count() > 0) 
     { 
      string[] columns = fileContent[0].Split('\t'); 
      for (int i = 0; i < columns.Count(); i++) 
      { 
       dtDataSource.Columns.Add(columns[i]); 
      } 
      for (int i = 1; i < fileContent.Count(); i++) 
      { 
       string[] rowData = fileContent[i].Split('\t'); 
       dtDataSource.Rows.Add(rowData); 
      } 
     } 
     return dtDataSource; 
    } 

此代碼工作正常,因爲我生成2003年的Excel文件(.xls)。

但是,如果我生成一個2007(.xlsx)格式通過改變代碼

Response.AddHeader("content-disposition", "attachment;filename=" + sFilename + ".xlsx"); 

我得到一個error like this

我做了我的功課,並且知道這個錯誤是因爲我的程序生成的.xlsx文件是通過使用HTML(標記語言)XML(標記語言)來完成的,這實際上應該用於2007 excel文件。

我的問題是我應該做些什麼改變才能獲得理想的結果,即我得到了2007年的Excel表格!

+0

你試圖代碼自己寫一個Excel文件的XML?你是否解壓縮了一個XLSX文件,並看到了什麼? – Brad

+1

你應該嘗試一下。有很多文件進入XLSX。雖然它基於XML,但並不像您想象的那麼簡單。 – Brad

+0

@brad我會這麼做! 但你能告訴我如何轉換Excel(.xlsx)格式的數據表! – Luv

回答

2

您必須使用擴展庫,我推薦使用EPPlus這是一個.net庫,讀取&使用Open Office Xml格式(xlsx)寫入Excel 2007/2010文件。 Library

,然後更換

protected void to_excel(object sender, EventArgs e) 
    { 
     string filepath = Path.Combine(Server.MapPath("~/Files"), fileupload.FileName); 
     fileupload.SaveAs(filepath); 
     string fname = fileupload.PostedFile.FileName; 
     DataTable dt = (DataTable)ReadToEnd(filepath); 
     string sFilename = fname.Substring(0, fname.IndexOf(".")); 
     sFilename = sFilename + ".xlsx"; 
     MemoryStream ms = DataTableToExcelXlsx(dt, "Sheet1"); 
     ms.WriteTo(HttpContext.Current.Response.OutputStream); 
     HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + sFilename); 
     HttpContext.Current.Response.StatusCode = 200; 
     HttpContext.Current.Response.End(); 
    } 

public void toexcel(DataTable dt, string Filename) 
    { 
     MemoryStream ms = DataTableToExcelXlsx(dt, "Sheet1"); 
     ms.WriteTo(HttpContext.Current.Response.OutputStream); 
     HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Filename); 
     HttpContext.Current.Response.StatusCode = 200; 
     HttpContext.Current.Response.End(); 
    } 
    public bool IsReusable 
    { 
     get { return false; } 
    } 
    public static MemoryStream DataTableToExcelXlsx(DataTable table, string sheetName) 
    { 
     MemoryStream Result = new MemoryStream(); 
     ExcelPackage pack = new ExcelPackage(); 
     ExcelWorksheet ws = pack.Workbook.Worksheets.Add(sheetName); 
     int col = 1; 
     int row = 1; 
     foreach (DataColumn column in table.Columns) 
     { 
      ws.Cells[row, col].Value = column.ColumnName.ToString(); 
      col++; 
     } 
     col = 1; 
     row = 2; 
     foreach (DataRow rw in table.Rows) 
     { 
      foreach (DataColumn cl in table.Columns) 
      { 
       if (rw[cl.ColumnName] != DBNull.Value) 
        ws.Cells[row, col].Value = rw[cl.ColumnName].ToString(); 
       col++; 
      } 
      row++; 
      col = 1; 
     } 
     pack.SaveAs(Result); 
     return Result; 
    } 

我得到了這個解決方案here

2

您可以創建Excel的多個工作表使用的數據集

using Excel = Microsoft.Office.Interop.Excel; 
using Microsoft.Office.Interop.Excel; 
using System.Data; 
using System.IO; 

[HttpPost] 
    public ActionResult CreateExcel(int id) 
    { 
     DataSet dataSet = new DataSet(); //Your Data Set 
     if (dataSet.Tables.Count > 0) 
     { 
      System.Data.DataTable dt1 = new System.Data.DataTable(); 
      System.Data.DataTable dt2 = new System.Data.DataTable(); 
      dt1 = dataSet.Tables[0]; 
      dt2 = dataSet.Tables[1]; 

      if (dt1.Rows.Count > 1 && dt2.Rows.Count > 1) 
      { 
       var excel = new Microsoft.Office.Interop.Excel.Application(); 
       var workbook = excel.Workbooks.Add(true); 

       AddExcelSheet(dt1, workbook); 
       AddExcelSheet(dt2, workbook); 

       //KK-Save the excel file into server path 
       string strLocation = "~/Upload/TempExcel/"; 
       string fName = "Report_" + DateTime.Now.ToString("MMddyyyyHHmmss") + ".xlsx"; 
       string strPath = Path.Combine(Server.MapPath(strLocation), fName); 
       workbook.SaveAs(strPath); 
       workbook.Close(); 

       //KK-Generate downloading link view page 
       System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response; 
       Response.ClearContent(); 
       Response.Clear(); 
       //Response.ContentType = "application/vnd.ms-excel"; //This is for office 2003 
       Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
       Response.AppendHeader("Content-Disposition", "attachment; filename=YourReport.xlsx"); 
       Response.TransmitFile(strPath); 
       Response.Flush(); 
       Response.End(); 

       //KK-Deleting the file after downloading 
       if (System.IO.File.Exists(strPath)) 
        System.IO.File.Delete(strPath); 
      } 
     } 

     return View(); 
    } 

    /// <summary> 
    /// KK-This method add new Excel Worksheet using DataTable 
    /// </summary> 
    /// <param name="ds"></param> 
    private static void AddExcelSheet(System.Data.DataTable dt, Workbook wb) 
    { 
     Excel.Sheets sheets = wb.Sheets; 
     Excel.Worksheet newSheet = sheets.Add(); 

     int iCol = 0; 

     foreach (DataColumn c in dt.Columns) 
     { 
      iCol++; 
      newSheet.Cells[1, iCol] = c.ColumnName; 
      newSheet.Cells[1, iCol].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.RoyalBlue); 
      newSheet.Cells[1, iCol].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); 
      newSheet.Cells[1, iCol].Font.Bold = true; 
      newSheet.Cells[1, iCol].BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin); 
     } 

     int iRow = 0; 
     foreach (DataRow r in dt.Rows) 
     { 
      iRow++; 

      // add each row's cell data... 
      iCol = 0; 

      foreach (DataColumn c in dt.Columns) 
      { 
       iCol++; 
       newSheet.Cells[iRow + 1, iCol] = r[c.ColumnName]; 
       newSheet.Cells[iRow + 1, iCol].BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin); 
      } 
     } 

    }