I want to open Excel file and save as text tab delimited format. How the object in Save As() Set?
Asked
Active
Viewed 836 times
1 Answers
0
here is how you can achieve this:
//The connection string to the excel file
string connstr = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=d:\temp\test.xls;Extended Properties=Excel 8.0";
//The connection to that file
OleDbConnection conn = new OleDbConnection(connstr);
//The query
string strSQL = "SELECT * FROM [Feuil1$]";
//The command
OleDbCommand cmd = new OleDbCommand(/*The query*/strSQL, /*The connection*/conn);
DataTable dt = new DataTable();
conn.Open();
try
{
OleDbDataReader dr1 = cmd.ExecuteReader();
StreamWriter sw = new StreamWriter("D:\\temp\\export.txt");
if (dr1.Read())
{
dt.Load(dr1);
}
int iColCount = 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)
{
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.Close();
Console.WriteLine("File is saved");
}
catch (OleDbException caught)
{
Console.WriteLine(caught.Message);
}
finally
{
conn.Close();
}
Source : Msdn

Kayani
- 942
- 7
- 23
-
the message error told "The 'Microsoft.Jet.Oledb.4.0' provider is not registered on the local machine." Did i forget something? @Kayani – Kenz Oct 08 '15 at 08:36
-
You may need to install this first http://www.microsoft.com/en-us/download/details.aspx?id=13255 – David B Oct 08 '15 at 08:54
-
see this question to resolve this issue : http://stackoverflow.com/questions/1991643/microsoft-jet-oledb-4-0-provider-is-not-registered-on-the-local-machine – Kayani Oct 08 '15 at 12:44