Click here to Skip to main content
16,018,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am building an application whish is converting .xlsx file to .csv file.An excel file contains 433 column but while converting its only taking 255 columns.Please provide me the solution by which i can read more than 255 columns.
Posted

Hi,

use this modified code working fine.

C#
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=""Excel 12.0;HDR=No;IMEX=1""", fpath);
        OleDbConnection conn = null;
        StreamWriter wrtr = null;
        OleDbCommand cmd = null;
        OleDbDataAdapter da = null;
       
            conn = new OleDbConnection(connectionString);
            conn.Open();
            cmd = new OleDbCommand("SELECT * FROM [test$]", conn);
            cmd.CommandType = CommandType.Text;
            wrtr = new StreamWriter("D:\\test.csv");

            da = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);


            string rowString = ""; ;
            for (int x = 0; x < dt.Rows.Count; x++)
            {
               
                rowString = "";
                for (int y = 0; y < dt.Columns.Count; y++)
                {
                    rowString += "\"" + dt.Rows[x][y].ToString() + "\",";   
                }
                wrtr.Write(rowString);
            }
            string str = rowString;
            wrtr.Dispose();
            conn.Close();
            MessageBox.Show("file is saved");
 
Share this answer
 
Comments
richa11 16-Dec-11 8:17am    
even this code is reading only 255 column.I need a code which read 433 columns.
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=""Excel 12.0;HDR=No;IMEX=1""", fpath);
OleDbConnection conn = null;
StreamWriter wrtr = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;
try
{
conn = new OleDbConnection(connectionString);
conn.Open();
cmd = new OleDbCommand("SELECT * FROM [test$]", conn);
cmd.CommandType = CommandType.Text;
wrtr = new StreamWriter("D:\\test.csv");

da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);



for (int x = 0; x < dt.Rows.Count; x++)
{

string rowString = "";
for (int y = 0; y < dt.Columns.Count; y++)
{
rowString += "\"" + dt.Rows[x][y].ToString() + "\",";

}
wrtr.WriteLine(rowString);
}
conn.Close();
MessageBox.Show("file is saved");

This is my code to convert the file
 
Share this answer
 
Hi,

Please tell us what is the process to follow you convert to excel file to cvs file.
if are you convert though code then you can put for loop for read data on excel file and insert data on cvs file and make some millisecond time difference to continue the loop.

if any problem show me your code!!
 
Share this answer
 
v2
Comments
richa11 15-Dec-11 6:23am    
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=""Excel 12.0;HDR=No;IMEX=1""", fpath);
OleDbConnection conn = null;
StreamWriter wrtr = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;
try
{
conn = new OleDbConnection(connectionString);
conn.Open();
cmd = new OleDbCommand("SELECT * FROM [test$]", conn);
cmd.CommandType = CommandType.Text;
wrtr = new StreamWriter("D:\\test.csv");

da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);



for (int x = 0; x < dt.Rows.Count; x++)
{

string rowString = "";
for (int y = 0; y < dt.Columns.Count; y++)
{
rowString += "\"" + dt.Rows[x][y].ToString() + "\",";

}
wrtr.WriteLine(rowString);
}
conn.Close();
MessageBox.Show("file is saved");

This is my code to convert the file
Improve solution Permalink | Posted 3 mins ago
try using range in the select query for reading the excel file.

Please do provide the right solution, if this doesn't work
 
Share this answer
 

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