0

Hy everyone i d like to ask some help for the next one .

i want to loadt a txt file into excel in c# with the workbooks.opentext method ! it runs well but in the text data there is some numbers wich contains "," character and thats the main separator of the whole text let me show u

'1200000','29,8','DUPAREC PAPÍRGYŰJTŐ ÉS FELD. KFT','

and the problem is in the second column .

Is there anyone who have any idea how to avoid this problem? my code is the next one

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
int[,] fieldInfo = new int[4, 2] { { 1, 2 }, { 2, 4 }, { 3, 2 }, { 4, 2 } };
object oFieldInfo = fieldInfo;
xlApp = new Excel.Application();
object missing = System.Reflection.Missing.Value;
xlApp.Workbooks.OpenText(
    filepath,Excel.XlPlatform.xlWindows,
    1,
    Excel.XlTextParsingType.xlDelimited,
    Excel.XlTextQualifier.xlTextQualifierNone,
    false,
    false,
    false,
    true,
    false,
    false,
    missing,
    oFieldInfo,
    missing,
    missing, 
    missing, 
    missing, 
    missing 
        );

Best regards !

Smith!

2
  • A bit unclear on what you are asking. Do you want to know how to avoid the , in numbers or how to export it text excel with that field? Commented Sep 16, 2013 at 12:31
  • i want to know how to "skipp" that character couse when the programm runs over there will be an extra collumn becouse of the split by comma, which one is not needed . i just want the columns wich is between the '' characters. Commented Sep 16, 2013 at 13:01

1 Answer 1

1

As your character to qualify a text is ' (single quote) you have to pass Excel.XlTextQualifier.xlTextQualifierSingleQuote as parameter.

See the Excel developer reference for more information.

The code below should make your import work.

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
int[,] fieldInfo = new int[4, 2] { { 1, 2 }, { 2, 4 }, { 3, 2 }, { 4, 2 } };
object oFieldInfo = fieldInfo;
xlApp = new Excel.Application();
object missing = System.Reflection.Missing.Value;
xlApp.Workbooks.OpenText(
    filepath,Excel.XlPlatform.xlWindows,
    1,
    Excel.XlTextParsingType.xlDelimited,
    Excel.XlTextQualifier.xlTextQualifierSingleQuote,
    false,
    false,
    false,
    true,
    false,
    false,
    missing,
    oFieldInfo,
    missing,
    missing, 
    missing, 
    missing, 
    missing 
        );

Because you defined the second column to be date time formatted in your fieldInfo array '29,8' will be displayed as 29. August in Excel.

You should replace {...,{2,4},...} with {...,{2,1},...} to let Excel parse the number as a number.

Sign up to request clarification or add additional context in comments.

4 Comments

the second column somhow changes into date type after the process:S '29,8' that one and in exel it seems 29.agust do u have any ide why?
Added solution for not parsing the way you want in my answer.
You are welcome. Please don't forget to mark the question as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.