Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am creating a web app that allows the user to choose which report they are trying to convert and upload the associated file. I am using the OleDbCommand and OleDbConnection to acess the .xls file. All the rows and columns of the .xls file show up when converted to the .txt file except for a column of decimal values. Can someone take a look and point me in the right direction? Thank you!

Here is the code I have so far:

if (ddlType.SelectedValue == "direct-debit")
        {
            string filename = FileUpload1.PostedFile.FileName.ToString();
            if (filename != "")
            {
                FileInfo file = new FileInfo(filename);
                if (file.Exists)
                {
                    Excel.Application xlApp;
                    Excel.Workbook xlWorkBook;
                    Excel.Worksheet xlWorkSheet;
                    object misValue = System.Reflection.Missing.Value;
                    xlApp = new Excel.ApplicationClass();
                    xlWorkBook = xlApp.Workbooks.Open(@"C:\directdebitTEST\directdebitTEST.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    //The connection string to the excel file
                    string connstr = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\directdebitTEST\directdebitTEST.xls;Extended Properties=Excel 8.0";
                    //The connection to that file
                    OleDbConnection conn = new OleDbConnection(connstr);
                    //The query
                    string strSQL = "SELECT * FROM [Sheet1$]";
                    //The command 
                    OleDbCommand cmd = new OleDbCommand(strSQL, conn);
                    DataTable dt = new DataTable();
                    conn.Open();
                    try
                    {
                        OleDbDataReader dr1 = cmd.ExecuteReader();
                        StreamWriter sw = new StreamWriter(@"C:\directdebitTEST\directdebitExport.txt");
                        if (dr1.Read())
                        {
                            dt.Load(dr1);
                        }
                        int iColCount = 3;
                            //dt.Columns.Count;
                        for (int i = 0; i < iColCount; i++)
                        {
                            sw.Write(dt.Columns[i]);
                            if (i < iColCount - 1)
                            {
                                sw.Write("\t");
                            }
                        }
                        sw.Write(sw.NewLine);
                        // Now write all the rows.
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dr.Field<string>("F1") != "User Group" && dr.Field<string>("F1") != "Batch Totals" && dr.Field<string>("F1") != "Grand Totals"
                                && dr.Field<string>("F1") != "Batch ID:" && dr.Field<string>("F1") != "Item Count")
                            {
                                sw.Write("6                           ");
                                for (int i = 0; i < iColCount; i++)
                                {
                                    if (!Convert.IsDBNull(dr[i]))
                                    {
                                        sw.Write(dr[i].ToString());
                                    }
                                    if (i < iColCount - 1)
                                    {
                                        sw.Write("\t");
                                    }
                                }
                                
                                sw.Write(sw.NewLine);
                            }
                        }
                        sw.Write("9");
                        sw.Close();
                    }
                    catch (OleDbException caught)
                    {
                        Response.Write(caught.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();
                    string fileName = "directdebitExport.txt";
                    string filePath = @"C:\directdebitTEST\directdebitExport.txt";
                    Response.Clear();
                    Response.AppendHeader("content-disposition",
                    "attachment; filename=" + fileName);
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(filePath);
                    Response.Flush();
                    Response.End();
                }
            }
            else
            {
                Response.Write("This file does not exist.");
            }
        }
Posted
Updated 15-May-12 8:49am
v2
Comments
Richard MacCutchan 15-May-12 15:09pm    
It would help if you gave us a clue as to where the conversion is and what happens there.
Richard C Bishop 15-May-12 16:17pm    
Well, I am creating a datatable "dt" with my OleDbCommand that selects everything from sheet1 of the .xls file. Once the datatable is created, I load an OleDbDataReader into the datatable and use a Streamwriter to write it out to a .txt file. So I guess there is not really any data conversion, just file type conversion.

Here is where the .xls file is read, streamwriter created and loading the datatable:

OleDbConnection conn = new OleDbConnection(connstr);
//The query
string strSQL = "SELECT * FROM [Sheet1$]";
//The command
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
DataTable dt = new DataTable();
conn.Open();
try
{
OleDbDataReader dr1 = cmd.ExecuteReader();
StreamWriter sw = new StreamWriter(@"C:\directdebitTEST\directdebitExport.txt");
if (dr1.Read())
{
dt.Load(dr1);
}
The code goes on to read all the rows and columns and then prompts the user to save the new file type. I hope this helped.
Richard MacCutchan 15-May-12 16:52pm    
Somewhere in here you are reading a "decimal" value or values, and (presumably) writing it out to your text file. How is that decimal value converted from a number to a text string?
Richard C Bishop 15-May-12 17:04pm    
Well it is not being converted, so is that my problem?
Richard MacCutchan 15-May-12 17:08pm    
Well that's pretty obvious. But you still have not answered my question: "How (or where) is that decimal value supposed to be converted from a number to a text string?".

1 solution

In my opinion, the connection string is wrong.
Replace:
C#
string connstr = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\directdebitTEST\directdebitTEST.xls;Extended Properties=Excel 8.0;";

with:
C#
string connstr = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\directdebitTEST\directdebitTEST.xls;Extended Properties=Excel 8.0;HDR=no;IMEX=0;";


Important note!

The quota " in the string needs to be escaped using your language specific escape syntax.
c#, c++   \"
VB6, VBScript   ""
xml (web.config etc)   &quot;
or maybe use a single quota '.

"HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite.

"IMEX=1;" tells the driver to always read "intermixed" (numbers, dates, strings etc) data columns as text. Note that this option might affect excel sheet write access negative.

SQL syntax "SELECT [Column Name One], [Column Name Two] FROM [Sheet One$]". I.e. excel worksheet name followed by a "$" and wrapped in "[" "]" brackets.

Check out the [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel] located registry REG_DWORD "TypeGuessRows". That's the key to not letting Excel use only the first 8 rows to guess the columns data type. Set this value to 0 to scan all rows. This might hurt performance. Please also note that adding the IMEX=1 option might cause the IMEX feature to set in after just 8 rows. Use IMEX=0 instead to be sure to force the registry TypeGuessRows=0 (scan all rows) to work.

If the Excel workbook is protected by a password, you cannot open it for data access, even by supplying the correct password with your connection string. If you try, you receive the following error message: "Could not decrypt file."


Source: http://www.connectionstrings.com/excel[^]

Try to debug program to find out what data are written into text file...
 
Share this answer
 
Comments
Richard C Bishop 15-May-12 17:02pm    
I changed the connection string to your suggested one and got the error:

"Could not find installable ISAM"

Forgive my ignorance but referring to your first sentence, what is "quota"?

Thank you.
Maciej Los 16-May-12 2:05am    
It could be: ' or " depend of programming language.

DataSet to TextFile
DataSet to TextFile tab limited

Did you debug your program? What value is in a decimal column?
Richard C Bishop 16-May-12 11:42am    
I initially used the connection string you had suggested and it did not work, but then I changed the IMEX=1 and it works now. Thank you for your help.
Maciej Los 16-May-12 15:44pm    
You're welcome
;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900