i am currently using open xml sdk to process document - just a hint.
and yes, you can ;-)
note jet is depricated ace is 32 and 64 bit but must be downloaded seperatly ...
to use jet change the connection string!
and... excel sheet names may have to start with a $
-- ^hell knows why!? --
and... to read csv you might want to use a special schema.ini file to get correct imports <- in case search on google about it
so far, try something like this:
OledbFileParser excelDataSet = new OledbFileParser(Properties.Settings.Default.CurrentExcelFile);
excelFileGridView1.DataSource = excelDataSet.Tables["excel.xlsx"];
class OledbFileParser : DataSet
{
public ArrayList SheetNames
{ get; private set; }
public FileInfo File
{ get; private set; }
public OledbFileParser(string FileName)
{
this.File = new FileInfo(Path.Combine(System.Windows.Forms.Application.StartupPath, FileName));
if (File.Exists)
{
this.DataSetName = File.Name;
SheetNames = new ArrayList();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
File.FullName.ToString() + ";Extended Properties=Excel 12.0;";
OleDbConnection con = new OleDbConnection(connectionString);
try
{
con.Open();
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "Table" });
for (int i = 0; i < dt.Rows.Count; i++)
{
SheetNames.Add(dt.Rows[i]["TABLE_NAME"].ToString());
Tables.Add(new System.Data.DataTable(SheetNames[i].ToString()));
string selectString = "SELECT * FROM [" + SheetNames[i] + "]";
OleDbCommand cmd = new OleDbCommand(selectString, con);
cmd.CommandType = CommandType.Text;
OleDbDataReader oleDataReader2 = cmd.ExecuteReader();
int cCnt = oleDataReader2.FieldCount;
for (int j = 0; j < cCnt; j++)
{
Tables[SheetNames[i].ToString()].Columns.Add(oleDataReader2.GetName(j).ToString());
}
string[] rowData = new string[cCnt];
while (oleDataReader2.Read())
{
for (int k = 0; k < cCnt; k++)
{
try
{
if (oleDataReader2.GetFieldType(k).ToString() == "System.Int32")
{
if (!oleDataReader2.IsDBNull(k)) { rowData[k] = oleDataReader2.GetInt32(k).ToString(); }
}
else if (oleDataReader2.GetFieldType(k).ToString() == "System.String")
{
if (!oleDataReader2.IsDBNull(k)) { rowData[k] = oleDataReader2.GetString(k); }
}
else if (!oleDataReader2.IsDBNull(k)) { rowData[k] = oleDataReader2.GetValue(k).ToString(); }
}
catch (Exception ex) { ErrorHandling.printUniversalErrorMassage(ex, "erro" + k); }
}
Tables[SheetNames[i].ToString()].Rows.Add(rowData);
}
}
}
catch (Exception ex) { ErrorHandling.printUniversalErrorMassage(ex, "read error"); }
finally
{
con.Dispose();
}
}
else
{
MessageBox.Show(File.FullName.ToString() + " not found");
}
}
}
writing may be a pain - i dont know. i used this clumsy code for contact import from outlook and it worked (well).