Click here to Skip to main content
15,907,236 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to load data from a CSV file using Microsoft.Office.Interop.Excel.

Here is the code I'm using:

C#
List<string> lstColumnsNames = new List<string>();

                    Microsoft.Office.Interop.Excel.Application objExcelApplication = new Microsoft.Office.Interop.Excel.Application();

                    objExcelApplication.Workbooks.OpenText(strFilePath,
                                                        DataType: Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,
                                                        TextQualifier: Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone,
                                                        ConsecutiveDelimiter: true,
                                                        Semicolon: true);

                    Microsoft.Office.Interop.Excel.Worksheet objExcelWorksheet = objExcelApplication.Workbooks[1].Sheets[strTableName];

                    if (objExcelWorksheet != null)
                    {
                        int columnCount = objExcelWorksheet.UsedRange.Columns.Count;
                        for (int i = 1; i < columnCount + 1; i++)
                        {
                            if (objExcelWorksheet.Cells[1, i].Value != null)
                            {
                                if (!lstColumnsNames.Contains(objExcelWorksheet.Cells[1, i].Value.ToString()))
                                    lstColumnsNames.Add(objExcelWorksheet.Cells[1, i].Value.ToString());
                            }
                        }
                    }

                    objExcelApplication.Workbooks[1].Close();


When I do load a file having a csv as extension, it does not really separate according to semicolons (while it is specified it should be). but when the same file is saved as a text file, it does.

Is there something missing?

Regards and thanks in advance.
Posted

1 solution

You may be missing some property due to which your code is not working as per your expectation. But if you think you haven't missed any property and the code is not working properly in case of csv file then I don't think it is a bad idea to change the extension of csv file to txt file before you pass that file to this method.
 
Share this answer
 
Comments
Mehdi_S 27-Aug-13 7:02am    
using just the text extension option is not really a good idea. I'm pretty sure that there is nothing missing but who knows, nothing is perfect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900