How to get zero values while importing datatables to excel sheet

Posted by Janrose under C# on 3/12/2015 | Points: 10 | Views : 1203 | Status : [Member] | Replies : 1
We are developing an automation tool in c#, we are using datatables and importing that datatables into excel sheet, while importing the values like "0056" we are finding difficulties here, becoz in excel sheet(output sheet) it was printing only "56" instead of "0056".

Please provide any solution for this...




Responses

Posted by: Yesi on: 3/29/2015 [Member] Starter | Points: 25

Up
0
Down
If your original data is 0056 you can try this .NET API http://www.e-iceblue.com/Introduce/free-dataexport-component.html , and it can keep your original data format.
Code here:
private void button1_Click(object sender, EventArgs e)

{
//connect database
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString @"Provider=""Microsoft.Jet.OLEDB.4.0"";Data Source=""demo.mdb"";User Id=;Password="
OleDbCommand command = new OleDbCommand();
command.CommandText = "select * from parts";
DataSet dataSet = new System.Data.DataSet();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command.CommandText,connection);
dataAdapter.Fill(dataSet);
DataTable t = dataSet.Tables[0];
//export datatable to excel
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
sheet.InsertDataTable(t, true, 1, 1);
book.SaveToFile("insertTableToExcel.xls");
System.Diagnostics.Process.Start("insertTableToExcel.xls");
}

Tutorials here: http://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Data-Export-/Import-Export-Datatable-to-Excel-from-Database.html .

Janrose, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response