Oledb excel extended properties

COM-based data access to Excel specifications are likely buried in nearly inaccessible Microsoft archive documentation. (Usually served as one enormous PDF)

The connectionstring has some parts:

Provider: It is the main oledb provider that is used to open the Excel
sheet. This will be Microsoft.Jet.OLEDB.4.0 for Excel 97 onwards Excel
file format and Microsoft.ACE.OLEDB.12.0 for Excel 2007 or higher
Excel file format (One with xlsx extension)

Data Source: It is the entire path of the Excel workbook. You need to mention a dospath that corresponds to an Excel file. Thus, it will
look like: Data Source=C:testApp.xls».

Extended Properties (Optional): Extended properties can be applied to Excel workbooks which may change the overall activity of the Excel
workbook from your program. The most common ones are the following:

HDR: It represents Header of the fields in the Excel table. Default is YES. If you don’t have fieldnames in the header of your
worksheet, you can specify HDR=NO which will take the columns of the
tables that it finds as f1,f2 etc.

ReadOnly: You can also open Excel workbook in readonly mode by specifying ReadOnly=true; By default, Readonly attribute is false, so
you can modify data within your workbook.

FirstRowHasNames: It is the same as HDR, it is always set to 1 ( which means true) you can specify it as false if you don’t have your
header row. If HDR is YES, provider disregards this property. You can
change the default behaviour of your environment by changing the
Registry Value
[HKLMSoftwareMicrosoftJet4.0EnginesExcelFirstRowHasNames] to 00
(which is false)

MaxScanRows: Excel does not provide the detailed schema defination of the tables it finds. It need to scan the rows before
deciding the data types of the fields. MaxScanRows specifies the
number of cells to be scanned before deciding the data type of the
column. By default, the value of this is 8. You can specify any value
from 1 — 16 for 1 to 16 rows. You can also make the value to 0 so that
it searches all existing rows before deciding the data type. You can
change the default behaviour of this property by changing the value of
[HKLMSoftwareMicrosoftJet4.0EnginesExcelTypeGuessRows] which is
8 by default. Currently, MaxScanRows is ignored, so you need only to
depend on TypeGuessRows Registry value. Hope Microsoft fixes this
issue to its later versions.

IMEX: (A Caution) As mentioned above, Excel will have to guess a number or rows to select the most appropriate data type of the
column, a serious problem may occur if you have mixed data in one
column. Say you have data of both integer and text on a single column,
in that case, Excel will choose its data type based on majority of the
data. Thus it selects the data for the majority data type that is
selected, and returns NULL for the minority data type. If the two
types are equally mixed in the column, the provider chooses numeric
over text.

For example, in your eight (8) scanned rows, if the column contains five (5) numeric values and three (3) text values, the
provider returns five (5) numbers and three (3) null values.

To work around this problem for data, set «IMEX=1» in the Extended Properties section of the connection string. This enforces
the ImportMixedTypes=Text registry setting. You can change the
enforcement of type by changing
[HKLMSoftwareMicrosoftJet4.0EnginesExcelImportMixedTypes] to
numeric as well.

Thus if you look into the simple connectionstring with all of them, it will look like:

    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\testexcel.xls;
    Extended Properties="Excel 8.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE""

    or:
    Copy Code

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\testexcel.xlsx;
    Extended Properties="Excel 12.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE""

We need to place extended properties into Quotes(«) as there are multiple number of values.

Содержание

  1. Как прочитать Excel с помощью OleDB (C#)
  2. Определение строки подключения
  3. Строка подключения для Excel 2007 и более новых версий
  4. Строка подключения для более ранних версий
  5. Как сделать SQL запрос из таблицы Excel
  6. maestrow / 1-post.md
  7. Specification of Extended Properties in OleDb connection string?
  8. 2 Answers 2
  9. In C# how to access excel headers using OLEDB (without Automation)?
  10. 6 Answers 6
  11. Linked
  12. Related
  13. Hot Network Questions
  14. Subscribe to RSS
  15. Microsoft ACE OLEDB 12.0 connection strings
  16. Connect to
  17. Excel 97
  18. Excel 97-2003 Xls files with ACE OLEDB 12.0
  19. Excel 2000
  20. Excel 97-2003 Xls files with ACE OLEDB 12.0
  21. Excel 2002
  22. Excel 97-2003 Xls files with ACE OLEDB 12.0
  23. Excel 2003
  24. Excel 97-2003 Xls files with ACE OLEDB 12.0
  25. Excel 2007
  26. Xlsx files
  27. Treating data as text
  28. Xlsb files
  29. Xlsm files
  30. Excel 2010
  31. Xlsx files
  32. Treating data as text
  33. Xlsb files
  34. Xlsm files
  35. Excel 2013
  36. Xlsx files
  37. Treating data as text
  38. Xlsb files
  39. Xlsm files
  40. Access 97
  41. Standard security (mdb file)
  42. With database password (mdb file)
  43. DataDirectory functionality (mdb file)
  44. Network Location (mdb file)
  45. Access 2000
  46. Standard security (mdb file)
  47. With database password (mdb file)
  48. DataDirectory functionality (mdb file)
  49. Network Location (mdb file)
  50. Access 2002
  51. Standard security (mdb file)
  52. With database password (mdb file)
  53. DataDirectory functionality (mdb file)
  54. Network Location (mdb file)
  55. Access 2003
  56. Standard security (mdb file)
  57. With database password (mdb file)
  58. DataDirectory functionality (mdb file)
  59. Network Location (mdb file)
  60. Access 2007
  61. Standard security
  62. With database password
  63. DataDirectory functionality
  64. Network Location
  65. Access 2010
  66. Standard security
  67. With database password
  68. DataDirectory functionality
  69. Network Location
  70. Access 2013
  71. Standard security
  72. With database password

Как прочитать Excel с помощью OleDB (C#)

В этой статье опишу как прочитать Excel с помощью OleDB.

Иногда бывает нужно вытянуть таблицу из Excel документа, записать в DataTable для последующей обработки.

Не всегда это удобно делать с помощью циклов, поэтому будем считывать таблицы, содержащиеся в документе и запрашивать из них данные с помощью SQL запросов.

Определение строки подключения

Для разных версий Excel будут свои строки подключения.

Строка подключения для Excel 2007 и более новых версий

Для работы с данными версиями необходимо установить Microsoft Access Database Engine 2010 Redistributable.

Так же C# может выбрасывать исключения по поводу недостающих драйверов. В этом случае необходимо скачать соответствующие драйверы с сайта Microsoft.

Строка подключения для более ранних версий

Строка подключения для Excel версии 2003 может иметь такой вид:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=<0>;Extended Properties=’Excel 8.0;HDR=Yes’

Если C# выбросит исключение, скачайте недостающий драйвер, Visual Studio подскажет, какой.

Как сделать SQL запрос из таблицы Excel

Для выполнения SQL запроса нужно найти таблицу в документе и выполнить к ней запрос:

На этом шаге мы имеем считанные данные из Excel документа в DataTable.

После обработки данных можно сохранить данные в другой документ или оформить сводную таблицу.

Так же можете посмотреть, как работать со сводными таблицами в Excel с помощью C# и редактора VBA, встроенного в Excel.

Источник

maestrow / 1-post.md

Для работы с Excel 2003 (.Xls) можно использовать провайдер Microsoft Jet OLE DB 4.0.

Для работы с Excel 2007 (.Xlsx) — Microsoft ACE OLEDB 12.0.

В Windows 10 открыть настройки источников данных ODBC можно написав «Источники данных ODBC» или через Панель управления Администрирование.

  • HDR=YES|NO . HDR=YES означает, что первую строку листа, следует рассматривать как заголовки колонок. Т.о. значение из первой строки можно использовать как имена полей в sql запросах (любых: select, insert, update, delete).
  • IMEX=1|3 . 1 — открыть соединение для чтения. 3 — для записи.

Создание Linked Server в Sql Server для доступа к Excel

После создания связанного сервера можно будет просмотреть имена доступных листов.

Затем, чтобы обратиться к сервису:

SELECT * FROM OPENQUERY (XLSX_2010, ‘Select * from [Sheet1$]’) или SELECT * FROM [XLSX_2010]. [Лист1$]

Обращение к лиcтам, диапазонам, полям

Для обращения к листу из SQL запроса нужно использовать имя листа, например: [Sheet1$] или [Лист1$] . Обращение к диапазону: [Sheet1$A16:F16] .

Вставка данных в произвольное место

Примеры указания диапазона при вставке строк ( insert )

При вставке должны выполняться следующие условия:

  • Первая строчка указанного диапазона дожна входить в диапазон ячеек с данными. Чтобы создать на листе диапазон с данными достаточно в углах некоторого прямоугольного диапазона (в левом верхнем и правом нижнем) вписать значение (C4:I7 на скриншоте). Т.е. сама первая строчка указанного в insert диапазона данные содержать не обязана, достаточно, чтобы она просто входила в такой диапазон. Иначе возникнет ошибка «This table contains cells that are outside the range of cells defined in this spreadsheet»
  • Хвост диапазона должен содержать пустые строки (хотя бы одну).

Пример: Дан лист, где заполнены только 2 ячейки: C4, I7. После выполнения команды INSERT INTO [table1$E6:G] VALUES(2, ‘FF’,’2014-01-03′) результат будет как на скриншоте. Поясним: строка E6:G6 является первой строкой диапазона для вставки. Она входит в заполненный диапазон C4:I7. Поэтому данные были вставлены на следующей пустой строке — 8. Из этого примера становится ясно, что через OleDb нельзя работать с несколькими независимыми диапазонами на одном листе, используя вставку ( update будет работать).

Источник

Specification of Extended Properties in OleDb connection string?

At the moment I’m searching for properties for a connection string, which can be used to connect to an Excel file in readonly mode. Searching Google gets me a lot of examples of connection strings, but I can’t seem to find a specification of all possibilities in the ‘Extended Properties’ section of the OleDb connection string.

At the moment I’ve this:

However. I’ve composed this by examples. So questions: 1. What is a decent source for OleDb Connection String documentation/reference? 2. Is the above connection string indeed connecting to the Excel file in readonly mode?

2 Answers 2

I am using UDL file for that.

  1. create empty file test.udl
  2. open it
  3. You will see Data Link Properties dialog
  4. On first tab change provider to Microsoft.Jet.OLEDB.4.0;
  5. Second tab select you Excel file
  6. Third tab set permission like Read
  7. On last tab set Extended Properties = ‘Excel 8.0; HDR=Yes’

Than save, and open file in text editor and you will see connection string

As well you can check msdn article ADO Provider Properties and Settings

COM-based data access to Excel specifications are likely buried in nearly inaccessible Microsoft archive documentation. (Usually served as one enormous PDF)

Copied to this answer for completeness:

The connectionstring has some parts:

Provider: It is the main oledb provider that is used to open the Excel sheet. This will be Microsoft.Jet.OLEDB.4.0 for Excel 97 onwards Excel file format and Microsoft.ACE.OLEDB.12.0 for Excel 2007 or higher Excel file format (One with xlsx extension)

Data Source: It is the entire path of the Excel workbook. You need to mention a dospath that corresponds to an Excel file. Thus, it will look like: Data Source=C:testApp.xls».

Extended Properties (Optional): Extended properties can be applied to Excel workbooks which may change the overall activity of the Excel workbook from your program. The most common ones are the following:

HDR: It represents Header of the fields in the Excel table. Default is YES. If you don’t have fieldnames in the header of your worksheet, you can specify HDR=NO which will take the columns of the tables that it finds as f1,f2 etc.

ReadOnly: You can also open Excel workbook in readonly mode by specifying ReadOnly=true; By default, Readonly attribute is false, so you can modify data within your workbook.

FirstRowHasNames: It is the same as HDR, it is always set to 1 ( which means true) you can specify it as false if you don’t have your header row. If HDR is YES, provider disregards this property. You can change the default behaviour of your environment by changing the Registry Value [HKLMSoftwareMicrosoftJet4.0EnginesExcelFirstRowHasNames] to 00 (which is false)

MaxScanRows: Excel does not provide the detailed schema defination of the tables it finds. It need to scan the rows before deciding the data types of the fields. MaxScanRows specifies the number of cells to be scanned before deciding the data type of the column. By default, the value of this is 8. You can specify any value from 1 — 16 for 1 to 16 rows. You can also make the value to 0 so that it searches all existing rows before deciding the data type. You can change the default behaviour of this property by changing the value of [HKLMSoftwareMicrosoftJet4.0EnginesExcelTypeGuessRows] which is 8 by default. Currently, MaxScanRows is ignored, so you need only to depend on TypeGuessRows Registry value. Hope Microsoft fixes this issue to its later versions.

IMEX: (A Caution) As mentioned above, Excel will have to guess a number or rows to select the most appropriate data type of the column, a serious problem may occur if you have mixed data in one column. Say you have data of both integer and text on a single column, in that case, Excel will choose its data type based on majority of the data. Thus it selects the data for the majority data type that is selected, and returns NULL for the minority data type. If the two types are equally mixed in the column, the provider chooses numeric over text.

For example, in your eight (8) scanned rows, if the column contains five (5) numeric values and three (3) text values, the provider returns five (5) numbers and three (3) null values.

To work around this problem for data, set «IMEX=1» in the Extended Properties section of the connection string. This enforces the ImportMixedTypes=Text registry setting. You can change the enforcement of type by changing [HKLMSoftwareMicrosoftJet4.0EnginesExcelImportMixedTypes] to numeric as well.

Thus if you look into the simple connectionstring with all of them, it will look like:

We need to place extended properties into Quotes(«) as there are multiple number of values.

Источник

This is my code where I am trying to access first row, first column

6 Answers 6

Did you try HDR=YES ? That’s what tells the OLEDB provider that you do have a header row.

Wouldn’t you want to set the HDR=No?

Telling the OLEDB provider that the first row contains headers will cause the provider to use the headers as the names for the fields. (I’m thinking about dumping the info into a datatable, after which you get the information @ DataTable.Columns[«[HEADER]»].Row. )

Since you’re using a simple data reader, and you want the «header» fields to be read as data, specify that these are not headers.

I’ve always used the built in functions of GetSchema to enumerate Sheets and Headers. It’s really quite slick and no non-sense. Good Luck!

I had a different problem, but was able to access the first row of Excel data and remove it via this OleDBAdapter Excel QA I posted via stack overflow.

If you are trying to access first row, first column, just populate a dataset per the post I mentioned and add to the bottom:

Linked

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.20.43331

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Microsoft ACE OLEDB 12.0 connection strings

Connect to

Excel 97

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyOldExcelFile.xls; Extended Properties = «Excel 8.0; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Excel 2000

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyOldExcelFile.xls; Extended Properties = «Excel 8.0; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Excel 2002

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyOldExcelFile.xls; Extended Properties = «Excel 8.0; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Excel 2003

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyOldExcelFile.xls; Extended Properties = «Excel 8.0; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Excel 2007

Xlsx files

Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Treating data as text

Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES; IMEX = 1»;

If you want to read the column headers into the result set (using HDR=NO even though there is a header) and the column data is numeric, use IMEX=1 to avoid crash.

To always use IMEX=1 is a safer way to retrieve data for mixed data columns. Consider the scenario that one Excel file might work fine cause that file’s data causes the driver to guess one data type while another file, containing other data, causes the driver to guess another data type. This can cause your app to crash.

Xlsb files

Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyBinaryExcel2007file.xlsb; Extended Properties = «Excel 12.0; HDR = YES»;

You can also use this connection string to connect to older 97-2003 Excel workbooks.

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Xlsm files

Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsm; Extended Properties = «Excel 12.0 Macro; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Excel 2010

Xlsx files

Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Treating data as text

Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES; IMEX = 1»;

If you want to read the column headers into the result set (using HDR=NO even though there is a header) and the column data is numeric, use IMEX=1 to avoid crash.

To always use IMEX=1 is a safer way to retrieve data for mixed data columns. Consider the scenario that one Excel file might work fine cause that file’s data causes the driver to guess one data type while another file, containing other data, causes the driver to guess another data type. This can cause your app to crash.

Xlsb files

Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyBinaryExcel2007file.xlsb; Extended Properties = «Excel 12.0; HDR = YES»;

You can also use this connection string to connect to older 97-2003 Excel workbooks.

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Xlsm files

Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsm; Extended Properties = «Excel 12.0 Macro; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Excel 2013

Xlsx files

Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Treating data as text

Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES; IMEX = 1»;

If you want to read the column headers into the result set (using HDR=NO even though there is a header) and the column data is numeric, use IMEX=1 to avoid crash.

To always use IMEX=1 is a safer way to retrieve data for mixed data columns. Consider the scenario that one Excel file might work fine cause that file’s data causes the driver to guess one data type while another file, containing other data, causes the driver to guess another data type. This can cause your app to crash.

Xlsb files

Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyBinaryExcel2007file.xlsb; Extended Properties = «Excel 12.0; HDR = YES»;

You can also use this connection string to connect to older 97-2003 Excel workbooks.

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Xlsm files

Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsm; Extended Properties = «Excel 12.0 Macro; HDR = YES»;

«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Access 97

Standard security (mdb file)

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:myFoldermyAccessFile.mdb; Jet OLEDB:Database Password = MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

DataDirectory functionality (mdb file)

Network Location (mdb file)

Access 2000

Standard security (mdb file)

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:myFoldermyAccessFile.mdb; Jet OLEDB:Database Password = MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

DataDirectory functionality (mdb file)

Network Location (mdb file)

Access 2002

Standard security (mdb file)

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:myFoldermyAccessFile.mdb; Jet OLEDB:Database Password = MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

DataDirectory functionality (mdb file)

Network Location (mdb file)

Access 2003

Standard security (mdb file)

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:myFoldermyAccessFile.mdb; Jet OLEDB:Database Password = MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

DataDirectory functionality (mdb file)

Network Location (mdb file)

Access 2007

Standard security

With database password

This is the connection string to use when you have an Access 2007 — 2013 database protected with a password using the «Set Database Password» function in Access.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:myFoldermyAccessFile.accdb; Jet OLEDB:Database Password = MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

Note! Reports say that a database encrypted using Access 2010 — 2013 default encryption scheme does not work with this connection string. In Access; try options and choose 2007 encryption method instead. That should make it work. We do not know of any other solution. Please get in touch if other solutions is available!

DataDirectory functionality

Network Location

Access 2010

Standard security

With database password

This is the connection string to use when you have an Access 2007 — 2013 database protected with a password using the «Set Database Password» function in Access.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:myFoldermyAccessFile.accdb; Jet OLEDB:Database Password = MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

Note! Reports say that a database encrypted using Access 2010 — 2013 default encryption scheme does not work with this connection string. In Access; try options and choose 2007 encryption method instead. That should make it work. We do not know of any other solution. Please get in touch if other solutions is available!

DataDirectory functionality

Network Location

Access 2013

Standard security

With database password

This is the connection string to use when you have an Access 2007 — 2013 database protected with a password using the «Set Database Password» function in Access.

Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:myFoldermyAccessFile.accdb; Jet OLEDB:Database Password = MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

Note! Reports say that a database encrypted using Access 2010 — 2013 default encryption scheme does not work with this connection string. In Access; try options and choose 2007 encryption method instead. That should make it work. We do not know of any other solution. Please get in touch if other solutions is available!

Источник

Excel 97

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyOldExcelFile.xls;Extended Properties="Excel 8.0;HDR=YES";

Excel 2000

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyOldExcelFile.xls;Extended Properties="Excel 8.0;HDR=YES";

Excel 2002

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyOldExcelFile.xls;Extended Properties="Excel 8.0;HDR=YES";

Excel 2003

Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyOldExcelFile.xls;Extended Properties="Excel 8.0;HDR=YES";

Excel 2007

Xlsx files

Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

Treating data as text

Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";

Xlsb files

Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyBinaryExcel2007file.xlsb;Extended Properties="Excel 12.0;HDR=YES";

Xlsm files

Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsm;Extended Properties="Excel 12.0 Macro;HDR=YES";

Excel 2010

Xlsx files

Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

Treating data as text

Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";

Xlsb files

Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyBinaryExcel2007file.xlsb;Extended Properties="Excel 12.0;HDR=YES";

Xlsm files

Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsm;Extended Properties="Excel 12.0 Macro;HDR=YES";

Excel 2013

Xlsx files

Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

Treating data as text

Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";

Xlsb files

Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyBinaryExcel2007file.xlsb;Extended Properties="Excel 12.0;HDR=YES";

Xlsm files

Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsm;Extended Properties="Excel 12.0 Macro;HDR=YES";

Access 97

Standard security (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Persist Security Info=False;

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Jet OLEDB:Database Password=MyDbPassword;

DataDirectory functionality (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|myAccessFile.mdb;Persist Security Info=False;

Network Location (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\serverNameshareNamefoldermyAccessFile.mdb;

Access 2000

Standard security (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Persist Security Info=False;

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Jet OLEDB:Database Password=MyDbPassword;

DataDirectory functionality (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|myAccessFile.mdb;Persist Security Info=False;

Network Location (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\serverNameshareNamefoldermyAccessFile.mdb;

Access 2002

Standard security (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Persist Security Info=False;

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Jet OLEDB:Database Password=MyDbPassword;

DataDirectory functionality (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|myAccessFile.mdb;Persist Security Info=False;

Network Location (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\serverNameshareNamefoldermyAccessFile.mdb;

Access 2003

Standard security (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Persist Security Info=False;

With database password (mdb file)

This is the connection string to use when you have an Access 97 — 2003 database protected with a password using the «Set Database Password» function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.mdb;Jet OLEDB:Database Password=MyDbPassword;

DataDirectory functionality (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|myAccessFile.mdb;Persist Security Info=False;

Network Location (mdb file)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\serverNameshareNamefoldermyAccessFile.mdb;

Access 2007

Standard security

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.accdb;Persist Security Info=False;

With database password

This is the connection string to use when you have an Access 2007 — 2013 database protected with a password using the «Set Database Password» function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;

DataDirectory functionality

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|myAccessFile.accdb;Persist Security Info=False;

Network Location

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\serversharefoldermyAccessFile.accdb;

Access 2010

Standard security

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.accdb;Persist Security Info=False;

With database password

This is the connection string to use when you have an Access 2007 — 2013 database protected with a password using the «Set Database Password» function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;

DataDirectory functionality

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|myAccessFile.accdb;Persist Security Info=False;

Network Location

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\serversharefoldermyAccessFile.accdb;

Access 2013

Standard security

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.accdb;Persist Security Info=False;

With database password

This is the connection string to use when you have an Access 2007 — 2013 database protected with a password using the «Set Database Password» function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:myFoldermyAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;

DataDirectory functionality

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|myAccessFile.accdb;Persist Security Info=False;

Network Location

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\serversharefoldermyAccessFile.accdb;

DBF / FoxPro

Standard

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:folder;Extended Properties=dBASE IV;User ID=Admin;

Read, update and delete

Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=2;RetrieveIds=Yes;DATABASE=http://mysharepointsite.com/documents/;LIST={5999B8A0-0C2F-4D4D-9C5A-D7B146E49698};

Read only

Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=1;RetrieveIds=Yes;DATABASE=http://mysharepointsite.com/documents/;LIST={5999B8A0-0C2F-4D4D-9C5A-D7B146E49698};

Write

Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=http://mysharepointsite.com/documents/;LIST={5999B8A0-0C2F-4D4D-9C5A-D7B146E49698};

  • Remove From My Forums
  • Вопрос

  • Доброго времени суток. Использую C# Visual Studio 2017Community.

    1. Создаю»cmd.ExecuteNonQuery()» таблицу EXCEL: Create table [Test](Record_type FirstName VarChar(27),TestName int)

    Строка подключения: conn.ConnectionString = «Provider=Microsoft.ACE.OLEDB.12.0; Data Source = » +
                                               
    testx + «; Extended Properties=Excel 12.0 Xml»;

    или

    conn.ConnectionString = «Provider=Microsoft.ACE.OLEDB.12.0; Data Source = » +
                                               
    testx + «; Extended Properties=»Excel 12.0 Xml;HDR=YES;IMEX=0″»;

    2. Заполняю адаптер:MyAdapter.Fill(MyDataSet); //Таблица содержит только одну строку заголовка.

    3.Проверяю тип данных для колонки «TestName «. Type type = MyDataSet.Tables[0].Columns[1].DataType;

    4. После заполнения числами столбца «TestName» «Insert into [CDR] (Record_type,TestName) Values (?,?)»

      MyAdapter.Update(MyDataSet);

    Excel отображает числа как строки, а нужно как числа.

    *************************************************************

    Для строк подключения ниже числа в Excel отображаются как числа :

    conn.ConnectionString = «Provider=Microsoft.Jet.OLEDB.4.0; Data Source = » +
                         testx + «; Extended Properties=Excel 8.0»;

    conn.ConnectionString = «Provider=Microsoft.ACE.OLEDB.12.0; Data Source = » +
                         testx + «; Extended Properties=»Excel 8.0;HDR=YES;IMEX=0″»;

    Для этих строк подключения  тип данных для колонки «TestName «. Type type = MyDataSet.Tables[0].Columns[1].DataType возвращается как «Double»

    в отличие от строки подключения где
    Extended Properties=Excel 12.0 Xml.

    **************************************************************************

    Пока не могу сделать чтобы числа отобразились как числа в Excel, а не как строки для Extended Properties=Excel 12.0 Xml.

Ответы

  • OLE DB ориентирован на работу с реляционными БД, где каждый столбец таблицы имеет определенный тип. Однако, документы Excel не являются реляционными БД; ячейки в столбце могут иметь смешанные типы, а формат
    отображения является вообще отдельным атрибутом, не имеющим отношение к данным. Когда вы подключаетесь к Excel-документу через OLE DB с параметром IMEX=0, драйвер БД пытается угадать тип столбца, анализируя некоторое
    количество начальных строк и выбирая тот, который встречается чаще. Если же IMEX=1, то драйвер должен воспринимать все как текст (но из-за бага в Jet это в действительности не работает, пока не установить определенные ключи в реестре). Способа принудительно
    воспринимать все как число нет.

    Словом, если вам нужен продвинутый контроль над типами данных, OLE DB — неподходящее средство. Используйте вместо него:

    Office Automation — для клиентских приложений, запускаемых
    на компьютерах с установленным Office

    Open XML SDK — для серверных приложений, или для приложений, запускаемых на компьютерах без Office

    • Помечено в качестве ответа

      15 марта 2019 г. 6:08

  • Насколько я помню, да, но я не уверен, распространяется это только на XLS или также и на XLSX. Вы можете проверить это сами, создав таблицу, в которой первые ~20 строк заняты числами, а далее идут текстовые значения, и попытавшись
    считать ее. При IMEX=1 все значения должны считаться как текст. Если числа считаются как числа, а текст — как NULL, это тот же баг, что и в Jet.

    • Помечено в качестве ответа
      Y_VS
      15 марта 2019 г. 6:06

  • Получилось. Оказалось это было из-за «conn.Close();».
    Когда я ранее проверял(Type type = MyDataSet.Tables[0].Columns[1].DataType;) какой тип у столбца, для которого установил «int» в команде «create table»,
    то получал «string» вместо «Double».

    *******************************

    cmd.ExecuteNonQuery(); //Создание таблицы Excel.
    conn.Close();
    MyAdapter.Fill(MyDataSet); //Заполнение адаптера заголовками.
    Type type = MyDataSet.Tables[0].Columns[1].DataType;
    Далее код где используется адаптер с командами Insert, заполнение DataTable и MyAdapter.Update(MyDataSet);.

    ********************************************

    Когда закомментировал conn.Close();, то после выполнения «Type type = MyDataSet.Tables[0].Columns[1].DataType;» тип стал Double.
    А «conn.Close();»нужно поставить обязательно после MyAdapter.Update(MyDataSet); Если поставить перед то работать не будет правильно.
    Получается что  для правильно сохранения нужно что бы было открыто соединение до окончания выполнения MyAdapter.Update(MyDataSet);.

    • Помечено в качестве ответа
      Y_VS
      15 марта 2019 г. 20:51

Провайдеры данных

Для работы с Excel 2003 (.Xls) можно использовать провайдер Microsoft Jet OLE DB 4.0.

SELECT * FROM OPENROWSET(
	'Microsoft.Jet.OLEDB.4.0', 
	'Excel 12.0;Database=d:tmpTimeSheets.xlsx;HDR=YES;IMEX=1', 
	'SELECT * FROM [Sheet1$]');

Для работы с Excel 2007 (.Xlsx) — Microsoft ACE OLEDB 12.0.

SELECT * FROM OPENROWSET (
    'Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0;Database=d:tmpTimeSheets.xlsx;HDR=YES;IMEX=1',
    'SELECT * FROM [Sheet1$]');

В Windows 10 открыть настройки источников данных ODBC можно написав «Источники данных ODBC» или через Панель управления Администрирование.

Extended Properties

  • HDR=YES|NO. HDR=YES означает, что первую строку листа, следует рассматривать как заголовки колонок. Т.о. значение из первой строки можно использовать как имена полей в sql запросах (любых: select, insert, update, delete).
  • IMEX=1|3. 1 — открыть соединение для чтения. 3 — для записи.

Создание Linked Server в Sql Server для доступа к Excel

EXEC sp_addLinkedServer
    @server= N'XLSX_2010',
    @srvproduct = N'Excel',
    @provider = N'Microsoft.ACE.OLEDB.12.0',
    @datasrc = N'd:tmpTimeSheets.xlsx',
    @provstr = N'Excel 12.0; HDR=Yes';
GO

После создания связанного сервера можно будет просмотреть имена доступных листов.

Затем, чтобы обратиться к сервису:

SELECT * FROM OPENQUERY (XLSX_2010, 'Select * from [Sheet1$]')
или
SELECT * FROM [XLSX_2010]...[Лист1$]

Обращение к лиcтам, диапазонам, полям

Для обращения к листу из SQL запроса нужно использовать имя листа, например: [Sheet1$] или [Лист1$]. Обращение к диапазону: [Sheet1$A16:F16].

Вставка данных в произвольное место

Примеры указания диапазона при вставке строк (insert)

  • [table1$B4:E20]
  • [table1$S4:U]
  • [table1$F:G]

При вставке должны выполняться следующие условия:

  • Первая строчка указанного диапазона дожна входить в диапазон ячеек с данными. Чтобы создать на листе диапазон с данными достаточно в углах некоторого прямоугольного диапазона (в левом верхнем и правом нижнем) вписать значение (C4:I7 на скриншоте). Т.е. сама первая строчка указанного в insert диапазона данные содержать не обязана, достаточно, чтобы она просто входила в такой диапазон. Иначе возникнет ошибка "This table contains cells that are outside the range of cells defined in this spreadsheet"
  • Хвост диапазона должен содержать пустые строки (хотя бы одну).

Пример: Дан лист, где заполнены только 2 ячейки: C4, I7. После выполнения команды INSERT INTO [table1$E6:G] VALUES(2, 'FF','2014-01-03') результат будет как на скриншоте. Поясним: строка E6:G6 является первой строкой диапазона для вставки. Она входит в заполненный диапазон C4:I7. Поэтому данные были вставлены на следующей пустой строке — 8. Из этого примера становится ясно, что через OleDb нельзя работать с несколькими независимыми диапазонами на одном листе, используя вставку (update будет работать).

Insert

Ошибки

  • System.Data.OleDb.OleDbException (0x80004005): Operation must use an updateable query. Соединение открыто для чтение, при этом происходит попытка внести изменения (выполнить insert, update или delete). Решение: открыть соединение для записи, установив свойство провайдера в строке соединения IMEX=3 (см. выше).
  • System.Data.OleDb.OleDbException (0x80004005): "This table contains cells that are outside the range of cells defined in this spreadsheet". Такая ошибка возникает при подпытке обновить (update) или вставить (insert) значения в диапазоне, в котором отсутствуют данные на листе.
    1. Если нужно произвести запись в определенные ячейки инструкцией update, то

Ссылки

  • https://www.codeproject.com/Tips/705470/Read-and-Write-Excel-Documents-Using-OLEDB
  • https://stackoverflow.com/questions/36987636/cannot-create-an-instance-of-ole-db-provider-microsoft-jet-oledb-4-0-for-linked
  • https://stackoverflow.com/questions/26267224/the-ole-db-provider-microsoft-ace-oledb-12-0-for-linked-server-null
  • http://www.ashishblog.com/importexport-excel-xlsx-or-xls-file-into-sql-server/
  • https://yoursandmyideas.com/2011/02/05/how-to-read-or-write-excel-file-using-ace-oledb-data-provider/
  • https://stackoverflow.com/questions/46373895/how-to-open-a-huge-excel-file-efficiently Несколько способов открыть большой Excel файл, в т.ч. с помощью OleDb.

В этой статье опишу как прочитать Excel с помощью OleDB.

Иногда бывает нужно вытянуть таблицу из Excel документа, записать в DataTable для последующей обработки.

Не всегда это удобно делать с помощью циклов, поэтому будем считывать таблицы, содержащиеся в документе и запрашивать из них данные с помощью SQL запросов.

oledb C#

Определение строки подключения

Для разных версий Excel будут свои строки подключения.

Строка подключения для Excel 2007 и более новых версий

//Можно использовать, если количество строк менее 65536
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'

//Если строк больше 65536
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties="Excel 12.0 Xml;HDR=YES";

Для работы с данными версиями необходимо установить Microsoft Access Database Engine 2010 Redistributable.

Так же C# может выбрасывать исключения по поводу недостающих драйверов. В этом случае необходимо скачать соответствующие драйверы с сайта Microsoft.

Строка подключения для более ранних версий

Строка подключения для Excel версии 2003 может иметь такой вид:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'

Если C# выбросит исключение, скачайте недостающий драйвер, Visual Studio подскажет, какой.

Как сделать SQL запрос из таблицы Excel

Для выполнения SQL запроса нужно найти таблицу в документе и выполнить к ней запрос:

//Объявляем OleDB соединение
using(OleDbConnection conn = new OleDbConnection(conStr))
{
  //Открываем подключение
  conn.Open();
  //Запрашиваем таблицы
  DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  DataRow schemaRow = schemaTable.Rows[0];
  //Получаеи имя таблицы
  string sheet = schemaRow["TABLE_NAME"].ToString();
  //Объявляем команду
  OleDbCommand com = conn.CreateCommand();
  //Создаем SQL запрос
  com.CommandText = "SELECT * FROM [" + sheet + "]";
  //Выполняем SQL запрос
  OleDbDataReader reader = com.ExecuteReader();
  //Записываем результат в DataTable
  DataTable table = new DataTable();
  table.Load(reader);
  //Выводим DataTable в таблицу на форму (если нужно)
  gridControl1.DataSource = table;
}

На этом шаге мы имеем считанные данные из Excel документа в DataTable.

После обработки данных можно сохранить данные в другой документ или оформить сводную таблицу.

Как работать с Excel с помощью C# обсуждалось ранее.

Так же можете посмотреть, как работать со сводными таблицами в Excel с помощью C# и редактора VBA, встроенного в Excel.


Просмотрено:
7 446

This reference section describes additional connection string information when using EDT to load data directly from an Excel spreadsheet file.

The Excel Database Tasks (EDT) software can load data from ANY source either as an Excel report,

or Validate and send the data to any destination Table or Stored Procedure. 

Supporting MS SQL Server, Oracle, MySQL, Access, DB2 databases.

Download EDT Free Trial

A connection string can be pasted into the EDT Data Source connection string text box as highlighted below.

After modifying the connection string,  click the Test button to verify the connection:

Microsoft ACE OLEDB 12.0

Microsoft ACE driver will allow you to query Office files (Including Access database AND Excel files)

ACE driver is available from Microsoft here:


Xlsx files 

Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;

Extended Properties=»Excel 12.0 Xml;HDR=YES»;

«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite.



Treating data as text

Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;

Extended Properties=»Excel 12.0 Xml;HDR=YES;IMEX=1″;

If you want to read the column headers into the result set (using HDR=NO even though there is a header) and the column data is numeric, use IMEX=1 to avoid crash.To always use IMEX=1 is a safer way to retrieve data for mixed data columns. Consider the scenario that one Excel file might work fine cause that file’s data causes the driver to guess one data type while another file, containing other data, causes the driver to guess another data type. This can cause your app to crash.


Xlsb files

Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.

Provider=Microsoft.ACE.OLEDB.12.0;

Data Source=c:myFoldermyBinaryExcel2007file.xlsb;

Extended Properties=»Excel 12.0;HDR=YES»;

You can also use this connection string to connect to older 97-2003 Excel workbooks.»HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.

Xlsm files
Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsm;

Extended Properties=»Excel 12.0 Macro;HDR=YES»;

«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite.


Excel 97-2003 Xls files with ACE OLEDB 12.0

You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyOldExcelFile.xls;

Extended Properties=»Excel 8.0;HDR=YES»;

«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite. 

Microsoft Jet OLE DB 4.0


Standard (Excel)

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:MyExcel.xls;

Extended Properties=»Excel 8.0;HDR=Yes;IMEX=1″;

How to Use JET in 64 bit environments 


Standard alternative

Try this one if the one above is not working. Some reports that Excel 2003 need the exta OLEDB; section in the beginning of the string.

OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:MyExcel.xls;Extended Properties=»Excel 8.0;HDR=Yes;IMEX=1″;

«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite.»IMEX=1;» tells the driver to always read «intermixed» (numbers, dates, strings etc) data columns as text. Note that this option might affect excel sheet write access negative.SQL syntax «SELECT [Column Name One], [Column Name Two] FROM [Sheet One$]». I.e. excel worksheet name followed by a «$» and wrapped in «[» «]» brackets.Check out the [HKEY_LOCAL_MACHINESOFTWAREMicrosoftJet4.0EnginesExcel] located registry REG_DWORD «TypeGuessRows». That’s the key to not letting Excel use only the first 8 rows to guess the columns data type. Set this value to 0 to scan all rows. This might hurt performance. Please also note that adding the IMEX=1 option might cause the IMEX feature to set in after just 8 rows. Use IMEX=0 instead to be sure to force the registry TypeGuessRows=0 (scan all rows) to work.If the Excel workbook is protected by a password, you cannot open it for data access, even by supplying the correct password with your connection string. If you try, you receive the following error message: «Could not decrypt file.»


.NET Framework Data Provider for OLE DB


Use an OLE DB provider from .NET

Provider=any oledb provider’s name;OledbKey1=someValue;OledbKey2=someValue;

See the respective OLEDB provider’s connection strings options. The .net OleDbConnection will just pass on the connection string to the specified OLEDB provider. 


Microsoft Excel 2007 ODBC Driver

Standard

Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};

DBQ=C:MyExcel.xlsx;


Standard (for versions 97 — 2003)

Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};

DBQ=C:MyExcel.xls;

Standard

Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=C:MyExcel.xls;

DefaultDir=c:mypath;

SQL syntax «SELECT [Column Name One], [Column Name Two] FROM [Sheet One$]». I.e. excel worksheet name followed by a «$» and wrapped in «[» «]» brackets.


Specify ReadOnly

[Microsoft][ODBC Excel Driver] Operation must use an updateable query. Use this connection string to avoid the error.

Driver={Microsoft Excel Driver (*.xls)};Dbq=C:MyExcel.xls;ReadOnly=0;

ReadOnly = 0 specifies the connection to be updateable. 


.NET Framework Data Provider for ODBC


Use an ODBC driver from .NET

Driver={any odbc driver’s name};OdbcKey1=someValue;OdbcKey2=someValue;

See the respective ODBC driver’s connection strings options. The .net Odbc Connection will just pass on the connection string to the specified ODBC driver.


.NET xlReader for Microsoft Excel


Excel file with header row

Data Source =c:myExcelFile.xlsx;HDR=yes;Format=xlsx;


Excel file without header row

Data Source =c:myExcelFile.xlsx;HDR=no;Format=xlsx;


Excel file with header row (for versions 97 — 2003)

Data Source =c:myExcelFile.xls;HDR=yes;Format=xls;


Excel file without header row (for versions 97 — 2003)

Data Source =c:myExcelFile.xls;HDR=no;Format=xls;

RSSBus ADO.NET Provider for Excel

Standard
Excel File=C:myExcelFile.xlsx;


No headers in Excel sheet

Excel File=C:myExcelFile.xlsx;Header=False;
Pseudo column names (A,B,C) are used instead. 


Caching data

Excel File=C:myExcelFile.xlsx;Cache Location=C:cache.db;Auto Cache=true;
Offline=false;
To retrieve data from the cache, add «#Cache» to the table name. For example, to query cached data from the «Sheet» table, execute «SELECT * FROM [Sheet#Cache]». 


Caching data and metadata

Excel File=C:myExcelFile.xlsx;Cache Location=C:cache.db;Auto Cache=true;
Offline=false;Cache Metadata=true;
The table metadata will also be cached instead of retrieving it from the data source. This improves connection performance. 


Cached data only / Offline mode

Excel File=C:myExcelFile.xlsx;Offline=true;Query Passthrough=true;
Cache Location=C:cache.db;
SELECT statements will always retrieve data from the cache. DELETE/UPDATE/INSERT statements is not allowed and will throw an exception. Excel 2000Excel 2002Excel 2003Excel 2007Excel 2010Excel 2013Excel 97


Using an External Cache Provider

RSSBus drivers have the ability to cache data in a separate database such as SQL Server or MySQL instead of in a local file using the following syntax:
Cache Provider=Provider.Namespace;
Cache Connection=’Connection String to Cache Database’;
Above is just an example to show how it works. It can be used both with «Auto Cache» and with «Cached Data Only / Offline Mode». 


Empty cells always NULL

Excel File=C:myExcelFile.xlsx;Empty Text Mode=EmptyAsNull;


Empty cells always empty string

Excel File=C:myExcelFile.xlsx;Empty Text Mode=NullAsEmpty;


Suppress formula calculation errors

Excel File=C:myExcelFile.xlsx;Ignore Calc Error=true;


Read «tilted sheets», where rows are headers and columns are rows

Excel File=C:myExcelFile.xlsx;Orientation=Horizontal;


Do not use formulas, only values

Do not treat values starting with equals (=) as formulas during inserts and updates.
Excel File=C:myExcelFile.xlsx;Allow Formula=false;

This article simplifies your work with MS Excel (both xls and xlsx) using Oledb and Microsoft Data Access. Simple demonstration to create/modify/delete Excel for both windows and web is provided.

  • Sample tool in C# with XLSX (Office 2007) support — 24.99 KB
  • Sample tool in C# — 24.61 KB
  • Sample tool in VB.NET with XLSX (Office 2007) Support — 26.88 KB
  • Sample tool for VB.NET — 26.28 KB
  • Sample CreateWorkBook download in ASP.NET — 21.62 KB

Table of Contents

  1. Introduction
  2. Available Ways to work with Excel Workbooks
  3. Background
  4. Working with Excel Workbook
    1. Anatomy of ConnectionString
    2. Creating Excel Workbook
    3. Getting Schema Definition
    4. Retrieve Data By Worksheet Name
    5. Retrieve Data Using Range
    6. Manipulating Data (Insert / Update / Delete)
    7. Drop Excel Worksheet
  5. Description and Usage of Sample Tool
  6. Code Explanation and Usage Info
  7. History

Introduction

Hi folks. It’s been a while since I wrote my last article. Meanwhile, I came across a lot of stuff, and want to share it with you. This article is regarding all we need to work with Excel through our programs.

While searching Google for this topic, I came across some of the links, but none of them gave a clear and concise idea of how to work with data in Excel in the easiest way from .NET. So I decided to jot down everything that may appear with this topic in this article.

To Work With Excel Workbooks, You Can Do Through Three Different Ways

You need 3rd party library which acts as an interface between your program and the Excel.

  1. You can make use of Excel InterOp Objects, but this requires you to have Excel installed in the development environment. This is a binding if you are going to make a product which is to be distributed.
  2. You can use OleDb data providers for Excel which comes for free with Windows. But there is one limitation though, you can access only data using this technique. You cannot do formatting through this technique.
  3. You can use XML to create Excel objects which will open in MSExcel correctly. This is easier, just you need to work with XML through programming. It also supports XML stylesheets. I will also try to discuss this in another article, for the time being, you may look into ExcelXMLDemo.

In this topic, I am going to discuss about the 3rd method which is the most common one that we use while working with Excel.

Background

Excel is the most common and popular format of showing data to the client nowadays. After the most common one (PDF), you need to place another format which may show the reports to the client. Excel would be the right choice for that. Now we often come up with a requirement to generate the data in an Excel Workbook. Recently while developing, I got one requirement to dump some data in Excel sheet. Thus I thought of writing this one.

Another important requirement is to read data from MS Excel 2007 format, which is also an unusual task to learn the entire structure of Excel 2007 objects. Using MDac, one can easily work with both of them without changing any of the code whatsoever.

Working with Excel Workbook

The rows and columns of Excel workbook closely resemble the rows and columns of a database table. We can use MDac (Microsoft Data Access Tool) that comes free with Windows update to work with Excel worksheet. In case of Excel Workbooks, each worksheet acts as a table and each workbook is actually a database. You can create, insert drop Excel objects through OleDb data clients from your program.

Now Let Us See How the connectionstring Will Look Like

Normal ConnectionString: (work for xls files)

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties="Excel 8.0;HDR=YES;""

Office 2007 ConnectionString : (work for xlsx files)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
Extended Properties="Excel 12.0;HDR=YES;""

Here, Data Source will be placed with a proper filename like C:\test.xls or C:\test.xlsx. If you want to create a workbook, just place the one that is not existing and use Create Table to create a workbook.

The connectionstring has some parts:

  1. Provider: It is the main oledb provider that is used to open the Excel sheet. This will be Microsoft.Jet.OLEDB.4.0 for Excel 97 onwards Excel file format and Microsoft.ACE.OLEDB.12.0 for Excel 2007 or higher Excel file format (One with xlsx extension)
  2. Data Source: It is the entire path of the Excel workbook. You need to mention a dospath that corresponds to an Excel file. Thus, it will look like: Data Source=C:\testApp.xls".
  3. Extended Properties (Optional): Extended properties can be applied to Excel workbooks which may change the overall activity of the Excel workbook from your program. The most common ones are the following:
    • HDR: It represents Header of the fields in the Excel table. Default is YES. If you don’t have fieldnames in the header of your worksheet, you can specify HDR=NO which will take the columns of the tables that it finds as f1,f2 etc.
    • ReadOnly: You can also open Excel workbook in readonly mode by specifying ReadOnly=true; By default, Readonly attribute is false, so you can modify data within your workbook.
    • FirstRowHasNames: It is the same as HDR, it is always set to 1 ( which means true) you can specify it as false if you don’t have your header row. If HDR is YES, provider disregards this property. You can change the default behaviour of your environment by changing the Registry Value [HKLMSoftwareMicrosoftJet4.0EnginesExcelFirstRowHasNames] to 00 (which is false)
    • MaxScanRows: Excel does not provide the detailed schema defination of the tables it finds. It need to scan the rows before deciding the data types of the fields. MaxScanRows specifies the number of cells to be scanned before deciding the data type of the column. By default, the value of this is 8. You can specify any value from 1 — 16 for 1 to 16 rows. You can also make the value to 0 so that it searches all existing rows before deciding the data type. You can change the default behaviour of this property by changing the value of [HKLMSoftwareMicrosoftJet4.0EnginesExcelTypeGuessRows] which is 8 by default. Currently, MaxScanRows is ignored, so you need only to depend on TypeGuessRows Registry value. Hope Microsoft fixes this issue to its later versions.
    • IMEX: (A Caution) As mentioned above, Excel will have to guess a number or rows to select the most appropriate data type of the column, a serious problem may occur if you have mixed data in one column. Say you have data of both integer and text on a single column, in that case, Excel will choose its data type based on majority of the data. Thus it selects the data for the majority data type that is selected, and returns NULL for the minority data type. If the two types are equally mixed in the column, the provider chooses numeric over text.

      For example, in your eight (8) scanned rows, if the column contains five (5) numeric values and three (3) text values, the provider returns five (5) numbers and three (3) null values.

      To work around this problem for data, set «IMEX=1» in the Extended Properties section of the connection string. This enforces the ImportMixedTypes=Text registry setting. You can change the enforcement of type by changing [HKLMSoftwareMicrosoftJet4.0EnginesExcelImportMixedTypes] to numeric as well.

      Thus if you look into the simple connectionstring with all of them, it will look like:

      Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\testexcel.xls;
      Extended Properties="Excel 8.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE""

      or:

      Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\testexcel.xlsx;
      Extended Properties="Excel 12.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE""

      We need to place extended properties into Quotes(«) as there are multiple number of values.

Can We Create Excel Workbook through this Technique?

If you are eager to know if we can create Excel workbook directly through OleDB, your answer is yes. The only thing that you need to do is to specify a non-existing file in the Data Source of the connectionstring.

string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;
                          Data Source=c:\testexcel.xls;
                          Extended Properties"Excel 8.0;HDR=YES"";
string createTableScript = "CREATE TABLE newTable(a1 MEMO,a2 INT,a3 CHAR(255))";
using(conObj = new OleDbConnection(connectionstring))
{
   using (OleDbCommand cmd = new OleDbCommand(createTableScript, conObj)
   {
     if (this.Connection.State != ConnectionState.Open) this.Connection.Open();
     cmd.ExecuteNonQuery();
   }
}

This will create a new workbook with one worksheet if the datasource file (testexcel.xls) is not existing in the location.

To Retrieve Schema Information of Excel Workbook

You can get the worksheets that are present in the Excel workbook using GetOleDbSchemaTable. Use the following snippet:

DataTable dtSchema = null;
dtSchema = conObj.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

Here dtSchema will hold the list of all workbooks. Say we have two workbooks: wb1, wb2. The above code will return a list of wb1, wb1$, wb2, wb2$. We need to filter out $ elements.

Selecting Data From a WorkBook (Specifying Range)

You can run a simple query to select Data from an Excel workbook. Say your workbook contains tables like w1, w2. Now, if write SELECT * FROM [w1] or SELECT * FROM 'w1', it will return you the whole datatable with all the data.

You Can Also Specify the Range of Selection, Just Write Query Like

SELECT * FROM [w1$A10:B10]

Thus it will select the data only from Excel Cell A10 : B10 Range.

string cmdText = "SELECT * FROM [w1$A10:B10]";
using(OleDbCommand cmd = new OleDbCommand(cmdText))
{
      cmd.Connection = this.Connection;
      OleDbDataAdapter adpt = new OleDbDataAdapter(cmd);
      DataSet ds = new DataSet();
      adpt.Fill(ds,"w1");
}

NOTE

A caution about specifying worksheets: The provider assumes that your table of data begins with the upper-most, left-most, non-blank cell on the specified worksheet. In other words, your table of data can begin in Row 3, Column C without a problem. However, you cannot, for example, type a worksheeet title above and to the left of the data in cell A1.

A caution about specifying ranges: When you specify a worksheet as your source, the provider adds new records below existing records in the worksheet as space allows. When you specify a range (named or unnamed), Jet also adds new records below the existing records in the range as space allows. However, if you re-query on the original range, the resulting recordset does not include the newly added records outside the range. Using MDAC, you cannot add new rows beyond the defined limits of the range, otherwise, you will receive Exception: "Cannot expand named range"

Running DML Statement

You can run any DML statement in the same way you do for other databases. Samples:

INSERT INTO [w1] VALUES('firsttextcol', 2, '4/11/2009', '10:20');

[We assume First Column is either memo or Char field, 2nd col is int, 3rd is Date, 4th is Time data type]

DELETE FROM [w1] Where secondintcol=2;
UPDATE [w1] SET secondintcol = 3 where firsttextcol = 'firsttextcol';

We can use [] (Square brackets) to allow spaces within columnnames and tablenames as we do for databases.

Droping Excel WorkSheet

To Drop an Excel Worksheet, Just Use

Drop Table [w1]

This will drop the worksheet.

If this is the last worksheet, it will not delete the workbook file. You need to do it yourself.

Using the Sample Tool

I have added one sample application that demonstrates the problem. It includes one class called ExcelObject which allows you to work with Excel. You can use the code to work in your own application easily.

Excel_data_access/cool_image.JPG

  1. Choose Browse and select an xls file. If you want to create the workbook, just click on Create table to Create a table with workbook.

    Excel_data_access/cool_image1.JPG

  2. Click on Retrieve to get the Tables present in the workbook. These are mainly worksheets.

    Excel_data_access/cool_image3.JPG

  3. You can create tables using the window. Just write the column name and click on Insert. Specify Tablename and a new worksheet will be created for you.

    Excel_data_access/cool_image4.JPG

  4. Generate Insert statements from the dynamic screen.

    Excel_data_access/cool_image5.JPG

  5. You can use Go to get the data loaded into the Grid.

NOTE

This is just a demo application. You can use the Class associated with the application call functions to do your job easy.

Using the Code

The code for ExcelObject Class will be like this:

using System.IO;
using System.Data.OleDb;
using System.Text;
using System.Data;
using System.Windows.Forms;

public class ExcelObject
{
     private string excelObject = = "Provider=Microsoft.{0}.OLEDB.{1};Data Source={2};
                                     Extended Properties="Excel {3};HDR=YES"";
     private string filepath = string.Empty;
     private OleDbConnection con = null;

        public delegate void ProgressWork(float percentage);
        private event ProgressWork Reading;
        private event ProgressWork Writeing;
        private event EventHandler connectionStringChange;

        public event ProgressWork ReadProgress
        {
            add
            {
                Reading += value;
            }
            remove
            {
                Reading -= value;
            }
        }

        public virtual void onReadProgress(float percentage)
        {
            if (Reading != null)
                Reading(percentage);
        }

        public event ProgressWork WriteProgress
        {
            add{ Writeing += value; }
            remove{ Writeing -= value; }
        }

        public virtual void onWriteProgress(float percentage)
        {
            if (Writeing != null)
                Writeing(percentage);
        }

        public event EventHandler ConnectionStringChanged
        {
            add{ connectionStringChange += value; }
            remove { connectionStringChange -= value; }
        }

        public virtual void onConnectionStringChanged()
        {
            if (this.Connection != null && 
                !this.Connection.ConnectionString.Equals(this.ConnectionString))
            {
                if (this.Connection.State == ConnectionState.Open)
                    this.Connection.Close();
                this.Connection.Dispose();
                this.con = null;
            }
            if (connectionStringChange != null)
            {
                connectionStringChange(this, new EventArgs());
            }
        }
        
        public string ConnectionString
        {
            get
            {
                if (!(this.filepath == string.Empty))
                {
                   
                    FileInfo fi = new FileInfo(this.filepath);
                    if (fi.Extension.Equals(".xls"))
                    {
                        
                        return string.Format(this.excelObject, 
                                   "Jet", "4.0", this.filepath, "8.0");
                    }
                    else if (fi.Extension.Equals(".xlsx"))
                    {
                        
                        return string.Format(this.excelObject, 
                                   "Ace", "12.0", Me.filepath, "12.0");
                    }
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        
        public OleDbConnection Connection
        {
            get
            {
                if (con == null)
                {
                    OleDbConnection _con = new OleDbConnection { 
                                ConnectionString = this.ConnectionString };
                    this.con = _con;
                }
                return this.con;
            }
        }

        public ExcelObject(string path)
        {
            this.filepath = path;
            this.onConnectionStringChanged();
        }
        
        public DataTable GetSchema()
        {
            DataTable dtSchema = null;
            if (this.Connection.State != ConnectionState.Open) this.Connection.Open();
            dtSchema = this.Connection.GetOleDbSchemaTable(
                   OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            return dtSchema;
        }
        
        public DataTable ReadTable(string tableName)
        {
            return this.ReadTable(tableName, "");
        }

        public DataTable ReadTable(string tableName, string criteria)
        {
            try
            {
                DataTable resultTable = null;
                if (this.Connection.State != ConnectionState.Open)
                {
                    this.Connection.Open();
                    onReadProgress(10);
                    
                }
                string cmdText = "Select * from [{0}]";
                if (!string.IsNullOrEmpty(criteria))
                {
                    cmdText += " Where " + criteria;
                }
                OleDbCommand cmd = new OleDbCommand(string.Format(cmdText, tableName));
                cmd.Connection = this.Connection;
                OleDbDataAdapter adpt = new OleDbDataAdapter(cmd);
                onReadProgress(30);
                
                DataSet ds = new DataSet();
                onReadProgress(50);
                
                adpt.Fill(ds, tableName);
                onReadProgress(100);
                
                if (ds.Tables.Count == 1)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                MessageBox.Show("Table Cannot be read");
                return null;
            }
        }
        
        public bool DropTable(string tablename)
        {
            try
            {
                if (this.Connection.State != ConnectionState.Open)
                {
                    this.Connection.Open();
                    onWriteProgress(10);                    
                }
                string cmdText = "Drop Table [{0}]";
                using (OleDbCommand cmd = new OleDbCommand(
                         string.Format(cmdText, tablename), this.Connection))
                {
                    onWriteProgress(30);
                    
                    cmd.ExecuteNonQuery();
                    onWriteProgress(80);                    
                }
                this.Connection.Close();
                onWriteProgress(100);
                
                return true;
            }
            catch (Exception ex)
            {
                onWriteProgress(0);
                
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        
        public bool WriteTable(string tableName, Dictionary<string, string> 
                                                             tableDefination)
        {
            try
            {
                using (OleDbCommand cmd = new OleDbCommand(
                this.GenerateCreateTable(tableName, tableDefination), this.Connection))
                {
                    if (this.Connection.State != ConnectionState.Open)
                    this.Connection.Open();
                    cmd.ExecuteNonQuery();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        
        public bool AddNewRow(DataRow dr)
        {
            using (OleDbCommand cmd = new OleDbCommand(
                          this.GenerateInsertStatement(dr), this.Connection))
            {
               cmd.ExecuteNonQuery();
            }
            return true;
        }
        
        private string GenerateCreateTable(string tableName, 
                            Dictionary<string, string> tableDefination)
        {
            StringBuilder sb = new StringBuilder();
            bool firstcol = true;
            sb.AppendFormat("CREATE TABLE [{0}](", tableName);
            firstcol = true;
            foreach (KeyValuePair<string, string> keyvalue in tableDefination)
            {
                if (!firstcol)
                {
                    sb.Append(",");
                }
                firstcol = false;
                sb.AppendFormat("{0} {1}", keyvalue.Key, keyvalue.Value);
            }

            sb.Append(")");
            return sb.ToString();
        }
        
        private string GenerateInsertStatement(DataRow dr)
        {
            StringBuilder sb = new StringBuilder();
            bool firstcol = true;
            sb.AppendFormat("INSERT INTO [{0}](", dr.Table.TableName);

            foreach (DataColumn dc in dr.Table.Columns)
            {
                if (!firstcol)
                {
                    sb.Append(",");
                }
                firstcol = false;

                sb.Append(dc.Caption);
            }

            sb.Append(") VALUES(");
            firstcol = true;
            for (int i = 0; i <= dr.Table.Columns.Count - 1; i++)
            {
                if (!object.ReferenceEquals(dr.Table.Columns[i].DataType, typeof(int)))
                {
                    sb.Append("'");
                    sb.Append(dr[i].ToString().Replace("'", "''"));
                    sb.Append("'");
                }
                else
                {
                    sb.Append(dr[i].ToString().Replace("'", "''"));
                }
                if (i != dr.Table.Columns.Count - 1)
                {
                    sb.Append(",");
                }
            }

            sb.Append(")");
            return sb.ToString();
        }
    }

After looking through the code you are clear that we are actually generating DDL and DML statements based on the Schema Definition. I know we can easily do this using OleDbCommandBuilder object, but I thought of making them myself. Functions exposed through this class are:

Methods

  • GetSchema: It returns the Schema defination datatable of the currently selected xls file. You can call this if you have connected with an existing Excel Workbook.
  • ReadTable: It automatically generates Select statement on the tablename passed and based on the criteria provided. It returns the DataTable of the currently selected Excel worksheet.
  • DropTable: Drops the table name passed, and which results in actual deletion of one worksheet from the workbook. The Function returns true if successful.
  • AddNewRow: This function creates an Insert statement and inserts a new row based on the DataRow passed in.

Properties

  • ConnectionString: You can get connectionstring of the filepath passed.
  • Connection: Returns OleDbConnection Object.

Events

  • ReadProgress: It generates a callback to the calling procedure on the percentage of Read of the file. You can handle this event to get the percentage progress value.
  • WriteProgress: Same as ReadProgress, only it is called during actual insert of data.
  • ConnectionStringChanged: This event occurs if FileName is changed somehow or a new file is created.

I have also provided the same class in VB.NET for those people who wants it in VB.NET.

You can find both of them from here:

Version 1

  • Download ExcelWrite_Csharp.zip — 24.61 KB
  • Download ExcelWrite_VB.NET.zip — 26.28 KB

Version 2

  • Download ExcelWrite_Csharp_V2.zip — 24.99 KB
  • Download ExcelWrite_VBNET_V2.zip — 26.88 KB
  • I have also added one example for ASP.NET users to dynamically create one Excel file and download it to clients.

You can find that from here:

  • Download ExcelDownload.zip — 21.62 KB

History

  • 7th June, 2009: First release
    • Looking forward to updating the article with new things. Hope you like this article.
  • 10th June, 2009: Second release
    • Support for xlsx files (Office 2007 Files). Hope this would help you.

1С Предприятие что это? 12
Что такое 1С?
1С — это фирма , у которой одно из направлений деятельности — разработка программного обеспечения для автоматизации бизнес-процессов предприятий. « 1С:Предприятие » — конкретный продукт, который выпускает компания 1С .
Что такое


Cклонения по падежам 6
НаКлиенте
Процедура Команда1(Команда)
ФИО = » Иванов Иван Иванович» ;
Падеж = 2;
Пол = 1;
Результат = СклонениеФИО(ФИО, Падеж, пол);
Сообщить(Результат);
КонецПроцедуры
НаСервере
Функция СклонениеФИО(ФИО, Падеж, пол)
Результат = » » ;


Excel файл как Внешний источник данных 16
Подключимся и загрузим из файла Excel данные в таблицу значений 1С. Сделать теперь это очень просто.
1. В конфигурации добавляет новый объект метаданных типа » Внешние источники данных» и назовем его просто » Excel» .
https://helpf.pro/uploads/img


Автоматическая установка ширины колонки табличного документа. 0
Процедура, которая » примерно» делает автоширину колонок (навроде, как если бы выделить все колонки и дважды щелкнуть мышкой по границе заголовка колонки). Привожу текст, работает неахти, но лучше чем ничего:
Процедура РасчетШириныКолонок(Табличный


Автоматическое резервное копирование 1С:Предприятия в облако с помощью ПО Effector Saver 4
Всем известно, для большей гарантии восстановления важных данных, необходимо копировать архивы в несколько мест хранения. Отдельный диск может помочь в случае порчи основного, но в случае если устройство будет потеряно или украдено, он будет так же


Посмотреть все результаты поиска похожих

Like this post? Please share to your friends:
  • Oledb excel connection string
  • Oledb data provider excel
  • Ole таблица excel в autocad
  • Ole работа с word
  • Ole открыть файл excel