使用File.WriteAllText()函数创build后,无法读取excel文件

我用datatable函数创build了一个excel表单。 我想用下面的连接string以编程方式读取excel表格。 这个string适用于所有其他excel工作表,但不是我使用函数创build的工作表。 我想这是因为excel版本问题。

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

任何人都可以提出一种方法,我可以创build一个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(); } } } 

你似乎正在写一个数据集为HtmlText,然后试图告诉它,这是一个Excel文件。 除非这是我错过了,这是不太可能的工作,因为Excel文件是一个特定的格式,所以需要写在这种格式。 如果你把你创build的文件,并试图打开它在Excel中,会发生什么?

解决方法之一是将数据写入CSV文件,可以使用Excel或OleDBConnection读取数据。

Folleed链接: C#Excel文件OLEDB读取HTML IMPORT

使用Extended Properties = \“HTML Import; HDR = No; IMEX = 1 select * from [tablename]

tablename从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;