A
Alain Clermont
Hi all,
I am a newby in Excel programming so my question could be an easy one.
I found lots of VB code snippets on this issue but almost none in C#.
I want to import in a new Excel 2003 doc a character delimited text
file.
Here is my code:
Excel.Workbook lWB =
this.excelApp.Workbooks.Add(System.Reflection.Missing.Value);
Excel.Worksheet lWS = (Excel.Worksheet)lWB.ActiveSheet;
string myfilename = "TEXT;C:\\tmp\\test.txt";
Excel.Range lRange = lWS.get_Range("A1","A1");
/////// the following call generates: System.ArgumentException - The
parameter is incorrect
Excel.QueryTable lQT = lWS.QueryTables.Add( myfilename, lRange,
"" );
lQT.Name = myfilename;
lQT.FieldNames = true;
lQT.RowNumbers = false;
lQT.FillAdjacentFormulas = false;
lQT.PreserveFormatting = true;
lQT.RefreshOnFileOpen = false;
lQT.RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells;
lQT.SavePassword = false;
lQT.SaveData = true;
lQT.AdjustColumnWidth = true;
lQT.RefreshPeriod = 0;
lQT.TextFilePromptOnRefresh = false;
lQT.TextFilePlatform = 437;
lQT.TextFileStartRow = 1;//172;
lQT.TextFileParseType = Excel.XlTextParsingType.xlFixedWidth;
lQT.TextFileTextQualifier =
Excel.XlTextQualifier.xlTextQualifierNone;
lQT.TextFileConsecutiveDelimiter = false;
lQT.TextFileTabDelimiter = false;
lQT.TextFileSemicolonDelimiter = false;
lQT.TextFileCommaDelimiter = false;
lQT.TextFileSpaceDelimiter = false;
lQT.TextFileColumnDataTypes = null;//new Array(xlGeneralFormat,
xlGeneralFormat,...);
lQT.TextFileFixedColumnWidths = null;//Array(19, 74, 4, 47, 13);
lQT.TextFileTrailingMinusNumbers = true;
lQT.TextFileOtherDelimiter = "|";
lQT.Refresh( false );
This C# code is inspired (copied!?!) from the various VB examples
found.
Questions:
- What should be provided as 3rd parameter to QueryTables.Add() ? Doc
mentions it is optional: all VB examples omit this param but this is
not an option in C# - must provide something.
Any help appreciated !
Thanks,
Alain
I am a newby in Excel programming so my question could be an easy one.
I found lots of VB code snippets on this issue but almost none in C#.
I want to import in a new Excel 2003 doc a character delimited text
file.
Here is my code:
Excel.Workbook lWB =
this.excelApp.Workbooks.Add(System.Reflection.Missing.Value);
Excel.Worksheet lWS = (Excel.Worksheet)lWB.ActiveSheet;
string myfilename = "TEXT;C:\\tmp\\test.txt";
Excel.Range lRange = lWS.get_Range("A1","A1");
/////// the following call generates: System.ArgumentException - The
parameter is incorrect
Excel.QueryTable lQT = lWS.QueryTables.Add( myfilename, lRange,
"" );
lQT.Name = myfilename;
lQT.FieldNames = true;
lQT.RowNumbers = false;
lQT.FillAdjacentFormulas = false;
lQT.PreserveFormatting = true;
lQT.RefreshOnFileOpen = false;
lQT.RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells;
lQT.SavePassword = false;
lQT.SaveData = true;
lQT.AdjustColumnWidth = true;
lQT.RefreshPeriod = 0;
lQT.TextFilePromptOnRefresh = false;
lQT.TextFilePlatform = 437;
lQT.TextFileStartRow = 1;//172;
lQT.TextFileParseType = Excel.XlTextParsingType.xlFixedWidth;
lQT.TextFileTextQualifier =
Excel.XlTextQualifier.xlTextQualifierNone;
lQT.TextFileConsecutiveDelimiter = false;
lQT.TextFileTabDelimiter = false;
lQT.TextFileSemicolonDelimiter = false;
lQT.TextFileCommaDelimiter = false;
lQT.TextFileSpaceDelimiter = false;
lQT.TextFileColumnDataTypes = null;//new Array(xlGeneralFormat,
xlGeneralFormat,...);
lQT.TextFileFixedColumnWidths = null;//Array(19, 74, 4, 47, 13);
lQT.TextFileTrailingMinusNumbers = true;
lQT.TextFileOtherDelimiter = "|";
lQT.Refresh( false );
This C# code is inspired (copied!?!) from the various VB examples
found.
Questions:
- What should be provided as 3rd parameter to QueryTables.Add() ? Doc
mentions it is optional: all VB examples omit this param but this is
not an option in C# - must provide something.
Any help appreciated !
Thanks,
Alain