2010-05-27 93 views
1

我已经使用函数从数据表创建了Excel表。我想用下面的连接字符串以编程方式读取excel表格。该字符串适用于所有其他Excel表单,但不适用于使用该函数创建的表单。我想这是因为excel版本问题。使用File.WriteAllText()函数创建后无法读取excel文件

OleDbConnection conn= new OleDbConnection("Data Source='" + path +"';provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;";); 

任何人都可以提出一种方法,我可以创建一个Excel表,使它可以再次使用上面的查询可读。我无法使用Microsoft InterOp库,因为它不受我的主机支持。我甚至改变了不同的编码格式。它仍然不起作用

public void ExportDataSetToExcel(DataTable dt) 
{ 
    HttpResponse response = HttpContext.Current.Response;   
    response.Clear(); 
    response.Charset = "utf-8"; 
    response.ContentEncoding = Encoding.GetEncoding("utf-8"); 
    response.ContentType = "application/vnd.ms-excel"; 
    Random Rand = new Random(); int iNum = Rand.Next(10000, 99999); 
    string extension = ".xls"; 
    string filenamepath = AppDomain.CurrentDomain.BaseDirectory + "graphs\\" + iNum + ".xls";   
    string file_path = "graphs/" + iNum + extension; 

    response.AddHeader("Content-Disposition", "attachment;filename=\"" + iNum + "\""); 
    string query = "insert into graphtable(graphtitle,graphpath,creategraph,year) VALUES('" + iNum.ToString() + "','" + file_path + "','" + true + "','" + DateTime.Now.Year.ToString() + "')"; 
    try 
    { 
     int n = connect.UpdateDb(query); 
     if (n > 0) 
     { 
      resultLabel.Text = "Merge Successfull"; 
     } 
     else 
     { 
      resultLabel.Text = " Merge Failed"; 
     } 
     resultLabel.Visible = true; 
    } 
    catch { }  
    using (StringWriter sw = new StringWriter()) 
    { 
     using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
     { 
      // instantiate a datagrid 
      DataGrid dg = new DataGrid(); 
      dg.DataSource = dt; //ds.Tables[0]; 
      dg.DataBind();     
      dg.RenderControl(htw); 
      File.WriteAllText(filenamepath, sw.ToString()); // File.WriteAllText(filenamepath, sw.ToString(), Encoding.UTF8); 
      response.Write(sw.ToString()); 
      response.End(); 
     } 
    } 
} 
+0

您是否确定创建的文档实际上包含某些内容和某些有意义的内容?看起来你正在尝试创建一个HTML文档,而不是一个Excel文档。 – 2010-05-27 10:45:08

回答

0

您似乎正在将数据集编写为HtmlText,然后试图告诉它它是一个Excel文件。除非这是我错过的东西,否则不太可能奏效,因为Excel文件是特定的格式,因此需要使用该格式编写。如果您创建了您创建的文件并尝试在Excel中打开它,会发生什么情况?

解决这个问题的一种方法是将数据写入CSV文件,该文件可以使用Excel或OleDBConnection读取。

+0

我也使用csv创建了该文件。但是我使用图中的数据来生成动态图。所以使用csv没有意义。 – 2010-05-27 11:07:01

+0

它通常在Microsoft Excel工作表中打开。但在保存文件时,它会要求转换为特定的格式。 – 2010-05-27 11:07:32

-1

Folowed链接: C# Excel file OLEDB read HTML IMPORT

使用 扩展属性= \“HTML进口; HDR =无; IMEX = 1 的SELECT * FROM [表名]

表名是由GetOleDbSchemaTable返回。

注意:这不会加载正常的excel ...用于该用途 扩展属性= \“Excel 8.0; HDR =否; IMEX = 1 \ 其中表名将带有$符号。

string full = "C:\\Temp.xls" 
      DataTable datatable = null; 
      string conString = ""; 
      OleDbConnection objConn = null; 

      try 
      { 
       //create the "database" connection string 
       connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + full + ";Extended Properties=\"HTML Import;HDR=No;IMEX=1\""; 

       objConn = new OleDbConnection(connString); 
       // Open connection with the database. 
       objConn.Open(); 
       // Get the data table containg the schema guid. 

       dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
      } 
      catch 
      { 
       throw exception 
      } 

      //no worksheets 
      if (dt == null) 
      { 
       DataCaptured = null; 
       return; 
      } 

      List<string> Sheets = new List<string>(); 

      // Add the sheet name to the string array. 

      foreach (DataRow row in dt.Rows) 
      { 
       string name = row["TABLE_NAME"].ToString(); 

       if (string.IsNullOrEmpty(name) == false) 
       { 
        Sheets.Add(name); 
       } 
      } 

      //no worksheets 
      if (excelSheets.Count == 0) 
      { 
       return; 
      } 

      Dataset dataSet = new DataSet(); 

      int sheetCount = excelSheets.Count; 

      for (int i = 0; i < sheetCount; i++) 
      { 
       string sheetName = excelSheets[i]; 

       OleDbDataAdapter ad = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", connString); 

       DataTable data = new DataTable(); 
       try 
       { 
        ad.Fill(data); 
       } 
       catch 
       { 
        throw exception 
       } 

       data.TableName = sheetName; 
       dataSet.Tables.Add(data); 

       adapter.Dispose(); 
      } 

      objConn.Close(); 

     return dataSet; 
+0

-1代码错误。 – tomfanning 2012-09-27 17:18:52