Проблем с Delphi и Excel
6 отговора
158 прегледа
Проблем с Delphi и Excel
Искам да отворя текстов файл със excela. Колоните са разделени с табулация(#9) и са 6 на брой.
Използвам следната конструкция:
Var
OfficeDocument: Variant;
OfficeDocument:= ExcelApplication.Workbooks;
OfficeDocument.OpenText(Filename:=WideString(Trim(FileName)),
Origin:=xlWindows,StartRow:=1,DataType:=xlDelimited,TextQualifier:=
xlDoubleQuote,ConsecutiveDelimiter:=False,Tab:=True,Semicolon:=False,
Comma:=False,Space:=False,Other:=False);
Файла се отваря без проблем. Всичките ми данни са текстови, но някой от тях представляват числа от вида; '0001'. Проблема е че excela ги
форматира като числа и '0001' става 1. Пробвах да генерирам макрос и той има следния вид:
Workbooks.OpenText Filename:= _
"C:\TEMP.XLS", Origin:=1254, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), TrailingMinusNumbers:=True
Очевидно: FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)) касае форматирането на всяка от колоните да е текстова.
Само че не знам как да интерпретирам тази конструкция на Delphi.
Разгледах сорса ExcelXP.pas
и там полето FieldInfo е от тип OleVariant.
Някакви предложения?
10x :) предварително
[линк]
Използвам следната конструкция:
Var
OfficeDocument: Variant;
OfficeDocument:= ExcelApplication.Workbooks;
OfficeDocument.OpenText(Filename:=WideString(Trim(FileName)),
Origin:=xlWindows,StartRow:=1,DataType:=xlDelimited,TextQualifier:=
xlDoubleQuote,ConsecutiveDelimiter:=False,Tab:=True,Semicolon:=False,
Comma:=False,Space:=False,Other:=False);
Файла се отваря без проблем. Всичките ми данни са текстови, но някой от тях представляват числа от вида; '0001'. Проблема е че excela ги
форматира като числа и '0001' става 1. Пробвах да генерирам макрос и той има следния вид:
Workbooks.OpenText Filename:= _
"C:\TEMP.XLS", Origin:=1254, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), TrailingMinusNumbers:=True
Очевидно: FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)) касае форматирането на всяка от колоните да е текстова.
Само че не знам как да интерпретирам тази конструкция на Delphi.
Разгледах сорса ExcelXP.pas
и там полето FieldInfo е от тип OleVariant.
Някакви предложения?
10x :) предварително
[линк]
Re: Проблем с Delphi и Excel
Само идея : пробвай да сложиш по един апостроф ( ' ) пред всяко поле, което може да се интерпретира като числово, но е текстово ( т.е. '0001 вместо 0001 )
_ _ _
Time is like a drug. Too much of it kills you.
_ _ _
Time is like a drug. Too much of it kills you.
Re: Проблем с Delphi и Excel
Stawa, no pri print ' ostawa, koeto, koeto e nepriatno!
Inache infoto za fieldinfo ot helpa na VBA e slednia:
FieldInfo Optional xlColumnDataType. An array containing parse information for individual columns of data. The interpretation depends on the value of DataType. When the data is delimited, this argument is an array of two-element arrays, with each two-element array specifying the conversion options for a particular column. The first element is the column number (1-based), and the second element is one of the XlColumnDataType constants specifying how the column is parsed.
XlColumnDataType can be one of these XlColumnDataType constants.
xlGeneralFormat General
xlTextFormat Text
xlMDYFormat MDY date
xlDMYFormat DMY date
xlYMDFormat YMD date
xlMYDFormat MYD date
xlDYMFormat DYM date
xlYDMFormat YDM date
xlEMDFormat EMD date
xlSkipColumn Skip Column
You can use xlEMDFormat only if you have installed and selected Taiwanese language support. The xlEMDFormat constant specifies that Taiwanese era dates are being used.
The column specifiers can be in any order. If there's no column specifier for a particular column in the input data, the column is parsed with the General setting. This example causes the third column to be skipped, the first column to be parsed as text, and the remaining columns in the source data to be parsed with the General setting.
Array(Array(3, 9), Array(1, 2))
If the source data has fixed-width columns, the first element in each two-element array specifies the position of the starting character in the column (as an integer; character 0 (zero) is the first character). The second element in the two-element array specifies the parse option for the column as a number between 1 and 9, as listed in the preceding table.
The following example parses two columns from a fixed-width text file. The first column includes characters 1 through 10. Characters 11, 12, 13, 14, and 15 are skipped. The second column includes character 16 through the last character in the line.
Array(Array(0, 1), Array(10, 9), Array(15, 1))
[линк]
Inache infoto za fieldinfo ot helpa na VBA e slednia:
FieldInfo Optional xlColumnDataType. An array containing parse information for individual columns of data. The interpretation depends on the value of DataType. When the data is delimited, this argument is an array of two-element arrays, with each two-element array specifying the conversion options for a particular column. The first element is the column number (1-based), and the second element is one of the XlColumnDataType constants specifying how the column is parsed.
XlColumnDataType can be one of these XlColumnDataType constants.
xlGeneralFormat General
xlTextFormat Text
xlMDYFormat MDY date
xlDMYFormat DMY date
xlYMDFormat YMD date
xlMYDFormat MYD date
xlDYMFormat DYM date
xlYDMFormat YDM date
xlEMDFormat EMD date
xlSkipColumn Skip Column
You can use xlEMDFormat only if you have installed and selected Taiwanese language support. The xlEMDFormat constant specifies that Taiwanese era dates are being used.
The column specifiers can be in any order. If there's no column specifier for a particular column in the input data, the column is parsed with the General setting. This example causes the third column to be skipped, the first column to be parsed as text, and the remaining columns in the source data to be parsed with the General setting.
Array(Array(3, 9), Array(1, 2))
If the source data has fixed-width columns, the first element in each two-element array specifies the position of the starting character in the column (as an integer; character 0 (zero) is the first character). The second element in the two-element array specifies the parse option for the column as a number between 1 and 9, as listed in the preceding table.
The following example parses two columns from a fixed-width text file. The first column includes characters 1 through 10. Characters 11, 12, 13, 14, and 15 are skipped. The second column includes character 16 through the last character in the line.
Array(Array(0, 1), Array(10, 9), Array(15, 1))
[линк]
Re: Проблем с Delphi и Excel
А нещо такова пробва ли :
_ _ _
Time is like a drug. Too much of it kills you.
type
tInArr = array[0..1] of integer;
tArr = array[0..5] of tInArr;
const
OleVar : tArr = ((1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1));
_ _ _
Time is like a drug. Too much of it kills you.
Я пробвай с това...
малко е крейзи - ама това ми хрумва на първо четене
FieldInfo := VarArrayOf([VarArrayOf([1, 1]), VarArrayOf([2, 1]), VarArrayOf([3, 1]), VarArrayOf([4, 1]), VarArrayOf([5, 1]), VarArrayOf( [6, 1])]);
Това е начина да създадеш VariantArray - погледни ако искаш и функцията VarArrayCreate([low,high], varType)
ама с нея няма да стане толкова лесно ;-)))
---
Колкото повече, толкова повече.
FieldInfo := VarArrayOf([VarArrayOf([1, 1]), VarArrayOf([2, 1]), VarArrayOf([3, 1]), VarArrayOf([4, 1]), VarArrayOf([5, 1]), VarArrayOf( [6, 1])]);
Това е начина да създадеш VariantArray - погледни ако искаш и функцията VarArrayCreate([low,high], varType)
ама с нея няма да стане толкова лесно ;-)))
---
Колкото повече, толкова повече.