Exporting Contacts into csv file automatically for

2019-05-31 09:02发布

Into my project when i m exporting Contacts into csv file automatically format mobile numbers starts with 91 into scientific.

e.g mobile no into exporting data

919433454320

mobile no into csv file when scientific format

9.194E+11

after format cell as number with 0 decimal places

919430000000

For exporting contacts into my project i m using Microsoft.Office.Interop.Excel.Application

Workbook here is my code

  Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            app.Visible = false;
            Workbook wb = app.Workbooks.Add();
            Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
            object[,] rawData = new object[DataTable.Rows.Count + 1, DataTable.Columns.Count];

            for (int col = 0; col < DataTable.Columns.Count; col++)
            {
                rawData[0, col] = DataTable.Columns[col].ColumnName;
            }

            for (int col = 0; col < DataTable.Columns.Count; col++)
            {
                for (int row = 0; row < DataTable.Rows.Count; row++)
                {
                    rawData[row + 1, col] = DataTable.Rows[row].ItemArray[col];
                }
            }
            string[] callchar = new string[]  {"range of my excel sheet (e.g from a to Bz"};
            string excelRange = callchar[0] + "1:" + callchar[DataTable.Columns.Count - 1] + (DataTable.Rows.Count + 1).ToString();
            Range r = ws.Cells.EntireColumn.NumberFormat;
            ws.get_Range(excelRange, Type.Missing).Value2 = rawData;
            wb.SaveAs(Filename: FilePath, FileFormat: XlFileFormat.xlCSVWindows, ReadOnlyRecommended: false);
            wb.Close();
            app.Quit();

1条回答
beautiful°
2楼-- · 2019-05-31 09:25

You can use late binding to export data:

            // try to open excel
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            Type typeExcelApplication = Type.GetTypeFromProgID("Excel.Application");
            if (typeExcelApplication == null)
                throw new Exception("Excel is not installed (Excel.Application is not found)");
            object objExcelApplication = Activator.CreateInstance(typeExcelApplication);
            object objWorkBooks = objExcelApplication.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, objExcelApplication, null);
            object objWorkBook = objWorkBooks.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objWorkBooks, null);
            object objWorkSheet = objWorkBook.GetType().InvokeMember("ActiveSheet", BindingFlags.GetProperty, null, objWorkBook, null);

            // fill data (put object obj into cell [1,1])
            objWorkSheet.GetType().InvokeMember("Cells", BindingFlags.SetProperty, null, objWorkSheet, new object[] { 1, 1, obj});

            // display excel
            typeExcelApplication.InvokeMember("Visible", BindingFlags.SetProperty, null, objExcelApplication, new object[] { true });
            Marshal.ReleaseComObject(objExcelApplication);

Excel should properly process types, so if you supply obj as a string - it will not try to make a number (scientific?) from it.

查看更多
登录 后发表回答