简体   繁体   中英

Mistake while formatting of cells with numbers by OleDbDataAdapter and Excel

Hallo, I have folowing code:

        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.xls;Extended Properties=\"Excel 8.0;Imex=1;HDR=False;\"");
        OleDbDataAdapter da = new("select * from [" + TableName + "$]", con);
        DataSet dsData = new System.Data.DataSet();

        con.Open();

        da.Fill(dsData); 

        dataGridView1.DataSource = dsData.Tables[0];

I try read the Data from file created by Excel 2007 SP2.

If my Excel-File (test.xls) is closed, the formatting of the cells with the numbers is right in the Table of dataGridView1.

Like this: 5,0 (formated with one comma place) shows as 5,0

If my Excel-File (test.xls) is opened, the formatting of the cells with the numbers is not right.

Like this: 5,0 shows as 5 !!!

Excel 2007 SP2 Microsoft Visual C# 2008 .Net Framework 2.0

Thank you for every help

I don't think you can set the formatting with OleDB, I think that only cares about the data. If you want to format the data as you want it you might have to fall back on Excel Automation where you'd have full control over that (possibly it might be more efficient to write the data using OleDB and then open the file with Automation and set the formatting).

If you would go down that route, here's a sample: How to automate Microsoft Excel from Microsoft Visual C#.NET

Though be aware that this is not recommended if running on a server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM