AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 12.04.2007, 16:10   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
gl00mie: Read Excel table via ADO
Источник: http://gl00mie.blogspot.com/2006/12/...e-via-ado.html
==============

I think it's quite a common task to read data from Excel files. A straight-forward way is to use Excel COM interfaces (Workbook, Worksheet, Range, Cell, etc), but damn it’s slow! On the other hand there is ADO (ActiveX Data Objects) that is fast, flexible and familiar to many developers. So it would be nice to access Excel files via ADO to speed up data import and simplify application code…

Here is a utility class that allows reading Excel worksheets as Recordsets. It uses ADOX.Catalog to collect Excel worksheet names and ADODB.Connection, ADODB.Recordset to access worksheet data with a simple "select * from [sheetname]" Recordset's command.

A typical scenario would be something like this:
Counter cnTotal = 0;
ItemId itemId;
ItemName itemName;
AmountCur price;
Filename strFilename;
container conSheets;
ExcelImportADO xlImport;
;
strFilename = @"c:\import.xls";
xlImport = new ExcelImportADO(strFilename);
try
{
// open the first sheet by default
if(!xlImport.openFile())
throw error(strfmt("Error opening Excel file «%1»", strFilename));
if(xlImport.getFieldsCount() < 3)
throw error(strfmt("Too few columns in the recordset:" +
" found %1, expected at least %2",
xlImport.getFieldsCount(), 3));
while(!xlImport.eof())
{
itemId = xlImport.getFieldValue(1);
itemName = xlImport.getFieldValue('ItemName');
price = xlImport.getFieldValue('ItemPrice', false);
// process data...
cnTotal++;
xlImport.moveNext();
}
xlImport.finalize();
Box::info(strfmt("%1 records read", cnTotal));
}
catch(Exception::Error)
{
xlImport.finalize();
}
You can download the source code of the class at axaptapedia.com. I would like to thank Gustav and blokva for inspiration and very useful tips on improving the class.
Note: ADOX.Catalog returns table names (Excel sheet names) in an alphabetical order - not in the order they appear in a worksheet, and by default the class uses the first name returned by ADOX.Catalog!

Источник: http://gl00mie.blogspot.com/2006/12/...e-via-ado.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
Теги
excel

 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Вспомогательный класс для импорта из Excel через ADO gl00mie DAX: База знаний и проекты 80 10.04.2017 10:55
dynamicsusers: DL Tips And Tricks: ADO Database Reader (Jet way) - Excel Example Blog bot DAX Blogs 0 02.08.2007 03:50
casperkamal: Using ADO to read from Excel in Microsoft Dynamics Ax Blog bot DAX Blogs 2 14.05.2007 11:59

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 11:35.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.