Cannot read excel file after creating it using File.WriteAllText () function

I created an excel sheet from a datatable using a function. I want to read an excel sheet programmatically using the following connection string. This line works fine for all other excel sheets, but not for the one I created using this function. I think this is due to a problem with the version of Excel.

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

Can anyone suggest a way that I can create an Excel sheet so that it can be read again using the above query. I cannot use the Microsoft InterOp library because it is not supported by my host. I even changed different encoding formats. However, it does not work.

 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();
        }
    }
}
+1
source share
2 answers

, HtmlText, , Excel. -, , , Excel . , , Excel, ?

- CSV, Excel, OleDBConnection.

0

: # Excel OLEDB HTML IMPORT

=\" HTML; HDR = ; IMEX = 1 * [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;
-1

All Articles