Driver microsoft excel driver xls xlsx xlsm xlsb dbq

 +14 

   

Распечатать

Подключимся и загрузим из файла Excel данные в таблицу значений 1С. Сделать теперь это очень просто.
1. В конфигурации добавляет новый объект метаданных типа «Внешние источники данных» и назовем его просто «Excel».
title

2. На закладке «Данные» созданного объекта «Excel» добавляем новую таблицу. При этом появится окно мастера добавления таблицы внешнего источника.
Выбираем первый пункт «Вручную», т.к. лично у меня второй пункт «Выбрать из списка таблиц внешнего источника данных» работает нестабильно.
title

Если Вы все же хотите попробовать добавить таблицу через второй пункт, то в качестве строки соединения в появившемся окне указываем:
Driver={Microsoft Excel Driver (*.xls)};DBQ=C:Documents and SettingsAdminМои документыfinance3.xls; DriverID=790
В этой строке укажите путь к своему файлу. Имя пользователя и пароль я не указывал.
Отмечу, что путь к файлу и строка соединения в дальнейшем не запоминается и нужны эти параметры единожды, чтобы создать автоматически описание полей таблицы источника.
Если соединение прошло нормально, то должен отобразиться список таблиц источника. Флажком можно выбрать нужную таблицу и ее поля.
3. Даем имя новой таблице «Данные». Переходим на вкладку таблицы «Данные». В поле «Имя в источнике данных» указываем имя листа в файле Excel и добавляем в конце знак «$». В моем случае это «Данные$». В табличной части «Поля» добавляем поля и редактируем их тип.
title

У каждого поля в свойствах должно быть задано как минимум «Имя» и «Имя в источнике данных».
4. Создаем процедуру в которой пишем подключение к источнику и запрос:

Код 1C v 8.х

 ПараметрыСоединения = Новый ПараметрыСоединенияВнешнегоИсточникаДанных; 
ПараметрыСоединения.СтрокаСоединения=
"Driver={Microsoft Excel Driver (*.xls)};DBQ=C:Documents and SettingsAdminМои документыfinance2.xls; DriverID=790";
ВнешниеИсточникиДанных.Excel.УстановитьОбщиеПараметрыСоединения(ПараметрыСоединения);
ВнешниеИсточникиДанных.Excel.УстановитьСоединение();

Запрос = Новый Запрос();

Запрос.Текст = "ВЫБРАТЬ
| Данные.date КАК Дата,
| Данные.time КАК Время,
| Данные.account КАК Счет,
| Данные.amount КАК Сумма,
| Данные.currency КАК Валюта,
| Данные.category КАК Категория,
| Данные.parent КАК КатегорияРодитель
|ИЗ
| ВнешнийИсточникДанных.Excel.Таблица.Данные КАК Данные";

ТЗ = Запрос.Выполнить().Выгрузить();

Вот и все!
Источник: passion-programmer


Параметры соединения для файла Excel выглядят вот так:

Файлы XLSX (версия Office 2007 и выше)
Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=D:ФайлExcel.xlsx;

Файлы XLS (ранее)
Driver={Microsoft Excel Driver (*.xls)}; DriverID=790; DBQ=D:ФайлExcel.xls;

With ODBC, you can summarise, and select just the data you need, in an Excel workbook before importing it into SQL Server. You can join data from different areas or worksheets. You can even get data from the result of a SQL Server SELECT statement into an Excel spreadsheet. Phil Factor shows how, and warns of some of the pitfalls.

Why Use ODBC?

It is reasonably easy to insert data from Excel into SQL Server, or the reverse, from any other ODBC database to any other, using PowerShell. The most important direction is from Excel to SQL Server, of course. It is quicker than automating Excel and you can do it without requiring a copy of Excel. It is neater than SSIS too, and more versatile. The most important thing, though, is that you can aggregate before you send the data. It is possible to do a lot of filtering and aggregation of data before it ever gets to SQL Server, since you can turn an existing Excel Workbook into a poor-man’s relational database, or even create one. This article will aim to show how this is done.

I always feel slightly awkward in talking about ODBC. It is a Once and Future technology, developed before its time, but now showing its value for processing large volumes of data, despite its quirks, poor documentation and lackluster support. If you use the ODBC driver, then your Excel workbook becomes a little SQL-based relational database. Worksheets, or areas within worksheets, become tables. There are some features missing, of course, but you can do joins between tables, filter rows to taste,  do aggregations and some string manipulations.  This means that you need pull far less data into SQL because you can do a lot of selection and pre-processing before the data gets anywhere near SQL server. If, for example, you only need the total, count, and variance of a day’s readings, then why on earth would you want to import more than those aggregated figures?  Even if you do, these aggregations, performed on the original data, can be used as a ‘reconciliation’ check that you’ve gulped all the data into their final destination without error.

 I also prefer to use ODBC and the sequential data reader to read data from Excel, or any other ODBC source, because it is fast; and I like to use the bulk copy library to insert  ODBC ‘reader’ data into a SQL Server table because it is extremely fast, so we’ll use that. When you have a large number of big spreadsheets to insert as a chore, then speed matters.

The ODBC Excel driver (ACE)

ODBC was conceived as a way of making it as easy to connect to a particular datasource such a relational database, text file, data document (e.g. XML), web-based data or spreadsheet

Currently, the state of the art in ODBC for Access and Excel is the Microsoft Access Database Engine 2010 Redistributable which can be downloaded here. This includes the more popular OLEDB drivers which run well in PowerShell too.  These drivers enable you to access a range of data files via SQL as if they were a relational database.  Formats include Access, CSV,  delimited, DBase and Excel 

For developing on a general-purpose 64-bit desktop computer, you’re likely to hit a very silly Microsoft muddle. Microsoft  recommends that you install the 32-bit version of Office 2010, even on 64-bit machines,  since many of the common Office Add-ins did not run in the 64-bit Office environment. This advice has become baked-in ‘best practice’.  If you are using 64-bit PowerShell, as most of us are, then you need to use the 64-bit version of the drivers. If you  only have the 32-bit Office on your machine, then it will already have the 32-bit drivers, which won’t be visible to 64-bit PowerShell, and won’t work.  You can’t install the 64 bit drivers when you already have the 32-bit drivers and I don’t think you can get anything good to happen by uninstalling the 32-bit drivers. Nope. All three (or four if you include Visual Studio) must be 64 bit. I gather that one way out of this Catch 22 is  to first install the 64-bit Office 2010 ODBC/OleDB drivers and after that the (32-bit) Office, but there is a better fix that involves tweaking the registry.  See this for the full frustrating story.

The ODBC Excel driver in ACE works with the latest Excel spreadsheet format up to 2010 as well as the old ones. I suspect that the latest version will work with Office 2013, though I haven’t yet tried it.

This driver  is valuable because of the flexibility it gives. It actually executes ODBC SQL, which is a subset of SQL92,  so you can put in column aliases, change the column order,  and filter out rows that you don’t want to import. In effect, it presents you with a SQL tables which can be named ranges, if it is an existing worksheet that you’ve added named ranges to.

Select * from MyNamedRange

More commonly, you can specify with a delimited worksheet name followed by a range, the range being a specification of the area of the worksheet just sufficient to enable the driver to find the data you want. If you leave out the range spec entirely, the entire worksheet becomes the table.

Select * from [MyWorksheet$]

If, for example, you wanted the data in the range from C3 to L8, you’d use the statement

Select * from [MyWorksheet$C3:M8]

In ODBC, if you specified, say,  row 8 as the end of the table, you can only select rows up to row 8, even if you have inserted more rows beyond that limit, as ODBC allows. If you use some flavours, such as  the old  MDAC ‘JET’ database engine,  then you cannot add new rows beyond the defined limits of a range, otherwise you will get the Exception: "Cannot expand named range" message

If you wanted to define your table as being between the columns C and L, starting at row 3 you’d use

Select * from [NameOfExcelSheet$C3:M]

If you do this, then there is no limit to the length of the table so you can insert as many rows as you like.  The ODBC provider adds new rows to the existing rows in the defined area as space allows

The dreaded connection string

Now, before we start doing interesting things with the ACE drivers, I ought to explain a bit about their connection strings. These contain the specification of the ODBC driver you wish to use, and the settings that you wish to transmit to the driver.

Ignoring, for the time being, the extended property settings, For Microsoft Office Access data, set the Connection String to

«Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ= MyPath/MyFile«

For Excel data, use

«Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=MyPath/MyFile«

For dBASE data, use

«Driver={Microsoft Access dBASE Driver {*.dbf, *.ndx, *.mdx)};DBQ=MyPath/MyFile«

For text data, use

«Driver={Microsoft Access Text Driver {*.txt, *.csv);DBQ=MyPath«

But you’re likely to want some extended properties for the settings to add a few details about the way that the ODBC provider should tackle this particular connection. Because the defaults can be changed globally in the registry, it is rather better to specify these extended properties rather than to rely on the defaults.

These extended properties are only relevant for the driver that you’re using. They are not always reliable and are poorly documented by Microsoft. I’ll only mention the essentials.

The driver needs to know if the first row of the table holds the name of the column. “HDR=Yes;” indicates that the first row contains column names, not data. It will actually just use the first 64 characters of the header.   “HDR=No;” treats the first row as data, but then the columns are named F1  onwards and you’d want to  alias them in your SQL statements to give them meaningful column names.

The Excel ODBC doesn’t keep a detailed schema definition of the tables. (the Text and Access  drivers by contrast do)  The ODBC Excel  driver will try to make sense of the data it finds by testing it to see what datatype it can use for the result. It does so by testing a number of rows before doing the import, and you can specify how many rows it tests before deciding the data type of the column by using MaxScanRows in the extended properties.  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, but this slows things down.

This is fine for a relational table but Excel often has mixed types in a column The ODBC Provider will try to return the data of the majority type, but return NULL values for the rest that won’t convert. If the two types are equally mixed in the column, the provider chooses numeric over text, and you lose all the text. Also, it will judge the length of the character datatype in the column from the first rows and if the first rows are less than 255 characters long it will truncate all the subsequent data to 255 characters even if cell values below are longer.

By setting the Import Mode (IMEX=1). You can force mixed data to be converted to text, but only when it finds mixed values on the rows that it checks.

You can also open the Excel workbook in read-only mode by specifying ReadOnly=true; By Default Readonly attribute is false, so you can modify data within your workbook. However, this will lock the entire workbook from access until you close the connection.

Let’s try it out.

Just so you can prove all this to yourself, I’ve supplied an Excel workbook that represents the old PUBS database that used to be distributed with SQL Server and Sybase. This means that you can use SQL from old examples that use PUBS and see what works. All you need to do is to convert the SQL Server version slightly by altering the names of the tables slightly to tell the driver that you want the entire worksheet of that name (the $ is the separator between the worksheet name and the range specification)

So let’s pop together a very simple test-rig to try things out in PowerShell. Be warned, I’ve set this up in read-write mode so it will update your spreadsheet in some circumstances (CUD). To play along, you’ll need to download my Excel version of the PUBS database and  alter the path to the excel file.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

set-psdebug -strict

$ErrorActionPreference = «stop»

$ExcelFilePath=‘MyPathpubs.xlsx’ #the full path of the excel workbook

if (!(Test-Path $ExcelFilePath))

{

Write-Error «Can’t find ‘$($ExcelFilePath)’. Sorry, can’t proceed because of this»

exit

}

try {

$Connection = New-Object system.data.odbc.odbcconnection

$Connection.ConnectionString = ‘Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=’+$ExcelFilePath+‘; Extended Properties=»Mode=ReadWrite;ReadOnly=false; HDR=YES»‘

$Connection.Open()

}

catch

{

$ex = $_.Exception

Write-Error «whilst opening connection to $ExcelFilePath : Sorry, can’t proceed because of this»

exit

}

try {

$Query = New-Object system.data.odbc.odbccommand

$Query.Connection = $connection

$Query.CommandText = @’

SELECT title, SUM(qty) AS sales,

COUNT(*) AS orders

FROM [titles$] t

INNER JOIN [sales$] s ON t.title_id=s.title_id

WHERE title like ‘%?’

GROUP BY title

ORDER BY SUM(qty) DESC

‘@

$Reader = $Query.ExecuteReader([System.Data.CommandBehavior]::SequentialAccess) #get the datareader and just get the result in one gulp

}

catch

{

$ex = $_.Exception

Write-Error «whilst executing the query ‘$($Query.CommandText)’ $ex.Message Sorry, but we can’t proceed because of this!»

$Reader.Close()

$Connection.Close()

Exit;

}

Try

{

$Counter = $Reader.FieldCount #get it just once

$result=@() #initialise the empty array of rows

while ($Reader.Read()) {

$Tuple = New-Object -TypeName ‘System.Management.Automation.PSObject’

foreach ($i in (0..($Counter 1))) {

Add-Member `

-InputObject $Tuple `

-MemberType NoteProperty `

-Name $Reader.GetName($i) `

-Value $Reader.GetValue($i).ToString()

}

$Result+=$Tuple

}

$result | Format-Table

}

catch

{

$ex = $_.Exceptio

Write-Error «whilst reading the data from the datatable. $ex.Message»

}

$Reader.Close()

$Connection.Close()

All these work

Inner joins

SELECT logo, pr_info, pub_name, city, state, country

FROM [pub_info$] pif INNER JOIN [publishers$] p

ON p.pub_id=pif.pub_id

Left or right outer joins

SELECT  title, stor_id, ord_num, qty,ord_date

FROM [titles$] t LEFT OUTER JOIN  [sales$] s

ON t.title_id=s.title_id

Expressions using columns

SELECT  fname+‘ ‘+ minit+‘ ‘+lname AS name, job_desc

FROM [jobs$] d

INNER JOIN  [employee$] e

ON d.job_id=e.job_id

Simple GROUP BY expression

SELECT COUNT(*) FROM [sales$] GROUP BY stor_ID

More complex aggregation with ORDER BY clause and a WHERE clause

SELECT  title, SUM(qty) AS sales,

COUNT(*) AS orders

FROM [titles$] t

INNER JOIN  [sales$] s ON t.title_id=s.title_id

WHERE title like ‘%?’

GROUP BY title

ORDER BY SUM(qty) DESC

String functions

SELECT  title, left(notes,20)+‘…’ as [note] FROM [titles$]

UNION and UNION ALL

SELECT  au_fname FROM [authors$] UNION ALL SELECT lname FROM [employee$]

One could go on and on; even subqueries work, but I think I’ve made the point that there is far more power in this ODBC Excel driver than just the facility for pulling out raw data. The same is true of the TEXT driver for OLEDB. It will do all this as well. To conform with the minimum syntax for ODBC, a driver must be able to execute CREATE TABLE, DELETE FROM (searched), DROP TABLE, INSERT INTO, SELECT, SELECT DISTINCT, and UPDATE (searched). SELECT statements can have WHERE and ORDER BY clauses. ACE does a bit better than this, since even the text driver allows SELECT INTO, and SELECT statements allow GROUP BY and HAVING. 

Creating a spreadsheet

You can, of course use the ODBC driver to create an Excel spreadsheet and write data into it. Here is the simplest working demo I can write without blushing. Be careful to ensure that the spreadsheet doesn’t exist as the whole point of the demo is to prove to you that it can create an entire spreadsheet workbook with several worksheets.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

$ExcelFilePath=‘MyPathNew.xlsx’ #the full path of the excel workbook

$Header= $true # we want your first row to be column headers

try {

$Connection = New-Object system.data.odbc.odbcconnection

$TheConnectionString = ‘Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=’+$ExcelFilePath+‘;Mode=ReadWrite;ReadOnly=false;Extended Properties=»HDR=’+«$(if ($Header){‘YES’}else{‘NO’})»+‘»‘

$Connection.ConnectionString=$TheConnectionString

$Connection.Open()

}

catch

{

$ex = $_.Exception

Write-Error «whilst opening connection to $ExcelFilePath using ‘$($TheConnectionString)’ : $ex.Message»

}

$Mycommand=$connection.CreateCommand()

$MyCommand.CommandText=«create table MyTable (MyColumn varchar, MyOtherColumn varchar)»

if ($Mycommand.ExecuteNonQuery() -eq -1)

{

$MyCommand.CommandText=«insert into MyTable (MyColumn, MyOtherColumn) select ‘myfirstRowCol’,’myFirstRowCol2′»

$rows=$Mycommand.ExecuteNonQuery()

«$rows rows inserted into worksheet MyTable»

}

$connection.Close()

Notice that I can’t create the table and do the insert in one batch as a command. One statement only can be used in the commandText.

Exploring your Excel metadata

You can find out what datatypes are available for any ODBC source, by using the OdbcConnection.GetSchema(string) method.

$Datatypes=$connection.GetSchema(‘DATATYPES’).TypeName

Which with my connection gives only the LOGICAL, CURRENCY, NUMBER, VARCHAR and DATETIME datatypes. More useful is..

$tables=$connection.GetSchema(‘TABLES’).Table_Name

 … that gives you a list of the available worksheets . The complete list, if you wish to peep at them, is

$connection.GetSchema(‘TABLES’)

$connection.GetSchema(‘DATATYPES’)

$connection.GetSchema(‘DataSourceInformation’)

$connection.GetSchema(‘Restrictions’)

$connection.GetSchema(‘ReservedWords’)

$connection.GetSchema(‘Columns’)

$connection.GetSchema(‘Indexes’)

$connection.GetSchema(‘Views’)

Hmm. This is beginning to look a bit more like a database. With the Columns MetadataCollection, you can find out as much as you’d ever want to know about the data that is available in the spreadsheet so if you want to read all the worksheets straight into SQL Server, this is a wide-open goal.

Creating Worksheets

Going back to the PUBS Excel database, let’s create a peoples table and populate it with both authors and salespeople. This has to be done in three gulps since the driver seems to dislike the idea of doing a batch, and it kicks when I try to UNION the two results.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

$ExcelFilePath=‘C:UsersAdministratorDocumentsPOSHScriptsPubs.xlsx’ #the full path of the excel workbook

$Header= $true # true if you want your first row to be read as column headers

if (!(Test-Path $ExcelFilePath))

{

Write-Error «Can’t find ‘$($ExcelFilePath)’.  Sorry, can’t proceed because of this»

exit

}

try {

$Connection = New-Object system.data.odbc.odbcconnection

$TheConnectionString = ‘Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=’+$ExcelFilePath+‘;Mode=ReadWrite;ReadOnly=false;Extended Properties=»HDR=’+«$(if ($Header){‘YES’}else{‘NO’})»+‘»‘

$Connection.ConnectionString=$TheConnectionString

$Connection.Open()

}

catch

{

$ex = $_.Exception

Write-Error «whilst opening connection to  $ExcelFilePath using ‘$($TheConnectionString)’ : $ex.Message»

}

$Mycommand=$connection.CreateCommand()

$MyCommand.CommandText=

  CREATE TABLE people

    (Person varchar)

«@

if ($Mycommand.ExecuteNonQuery() -eq -1)

{$MyCommand.CommandText=

    INSERT into [people$](person)

       SELECT lname FROM [employee$]

«@

$rows=$Mycommand.ExecuteNonQuery()

$MyCommand.CommandText=

    INSERT into [people$](person)

       SELECT  au_fname FROM [authors$]

«@

$rows=$rows+$Mycommand.ExecuteNonQuery()

}

«$rows rows inserted into table»

$connection.Close()

You’ll find you can UPDATE, INSERT and DELETE data perfectly happily this way.  If you connect up a spreadsheet to a SQL Server database, then you can have a lot of fun copying entire databases into spreadsheets, and back again. Robyn and I show how to do this here.

The problem is in the Workbook you create. Whether you name it XLS or XSLX it produces an XLSX spreadsheet, in the latest zipped Office Open XML form.  The trouble is that, with my version of the driver,  I can only get  Excel to read it with the XLS filetype, since it says that there is an error if you try to open it as an .XLSX file. I suspect that the ODBC driver hasn’t been that well tested by Microsoft.

Getting data into SQL Server from Excel using PowerShell

Now, what about using PowerShell to copy the data, maybe filtered, sorted and aggregated, into SQL Server, using PowerShell and ODBC. In this direction we can save a lot of time by using the BCP library. We’ll now describe the routine.

We’ll keep this unpacked, as a script rather than a function, since this is designed to illustrate the process.

We’ll start by defining our credentials, preferences, sources and destinations. We’ll read in the data from and excel spreadsheet and then spit it out into SQL Server, creating a table if necessary. To create the destination table (some of these spreadsheets are rather wide and therefore easier to import automatically), we’ll need to examine the metadata, and to interpret this to the SQL Server equivalent, so we’ll do that. To use the BCP library, it is good to have an indication of progress so I’ll show how you do that.

I’ve provided the sample data so that you don’t have to scramble around to find something suitable. This is some climate data, which is handy for checking things like date conversion.

You will notice that although you can render numbers in a variety of ways, there is only one way of storing numbers in Excel, in the ‘NUMBER‘ datatype (the other datatypes in Excel are LOGICAL, CURRENCY, VARCHAR and DATETIME).  I’ve therefore had to specify the precision of numeric data, which is tough if you have some columns with integers and others with real decimal data with numbers after the decimal point (scale). Remember that this routine is just creating a staging table, not the final destination. All you need to do is to add your own statements to transfer the data to their final table with the CAST to the correct internal data type!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

set-psdebug -strict

$ErrorActionPreference = «stop»

$ExcelFilePath = ‘MyPathCambridgeWeatherData.xlsx’ #the full path of the excel workbook

$Worksheet = ‘cambridgedata’ #this is the actual worksheet where the data is

$DataRange = » #e.g. ‘A2:M33’  this is the range of the cells that make up the table. leave blank to read the whole worksheet

# leave out the second row number to read all rows from the column range

$Header = $true # true if you want your first row to be read as column headers

# If you aren’t reading columns they are labelled F1..n. You can easily specify them

#$ColumnNames=»’2011» as year,[F1] as Day’

$ColumnNames = ‘*’

#If you dont have fieldnames in the header of your worksheet, you can specify $Header= $false and use F1..Fn instead.

$DestinationTable = ‘CambridgeClimateData’ #the name of the SQL Server table where you want to put the data

$Destinationinstance = ‘MyInstance’ #the name of the server or instance

$Destinationdatabase = ‘MyDataBase’ #the name of the datatabase where you want to put the data

$DestinationWindowsSecurity = $true #or $False if you aren’t using Windows security

$DestinationUserID = » #the name of the SQL Server user if not integrated security

$DeleteContentsOfTableBeforeCopy = $false

$PrecisionForNumericData = 1

if (!(Test-Path $ExcelFilePath))

{

    Write-Error «Can’t find ‘$($ExcelFilePath)’.  Sorry, can’t proceed because of this»

    exit

}

try

{

    $Connection = New-Object system.data.odbc.odbcconnection

    $TheConnectionString = ‘Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=’ + $ExcelFilePath + ‘; Extended Properties=»READONLY=TRUE; HDR=’ + «$(if ($Header) { ‘YES’ }

        else { ‘NO’ })» + ‘»‘

    $Connection.ConnectionString = $TheConnectionString

    $Connection.Open()

}

catch

{

    $ex = $_.Exception

    Write-Error «whilst opening connection to  $ExcelFilePath using ‘$($TheConnectionString)’ :  $($ex.Message). Sorry, can’t proceed because of this»

    exit

}

# get the types via $Connection.GetSchema(‘DataTypes’)|select TypeName, DataType,SQLType

try

{

    $Query = New-Object system.data.odbc.odbccommand

    $Query.Connection = $connection

    $Query.CommandText = ‘Select’ + $columnNames + ‘ from [‘ + $Worksheet + ‘$’ + $DataRange + ‘]’

    $Reader = $Query.ExecuteReader([System.Data.CommandBehavior]::SequentialAccess) #get the datareader and just get the result in one gulp

}

catch

{

    $ex = $_.Exception

    Write-Error «whilst making the query ‘$($Query.CommandText)’ $ex.Message Sorry, but we can’t proceed because of this!»

    Exit;

}

$columns = $reader.GetSchemaTable() | select columnName, datatype

if ($DeleteContentsOfTableBeforeCopy) { $deletionScript = «ELSE DELETE from $DestinationTable « }

else { $deletionScript = » }

$CreateScript =

IF NOT EXISTS

(select TABLE_NAME from information_schema.tables

    where TABLE_NAME like ‘$DestinationTable’)

CREATE TABLE $DestinationTable (

«@

$CreateScript += $columns | foreach-object{ $datatype = «$($_.dataType)»; «`n`t[$($_.columnName.Trim())]  $(switch ($dataType) { ‘double'{ «numeric(18,$PrecisionForNumericData)» } ‘boolean'{ ‘int’ } ‘decimal'{ ‘Money’ } ‘datetime'{ ‘DateTime’ }

            default { ‘NVARCHAR(MAX)’ } }),» }

$CreateScript = $CreateScript.Substring(0, $CreateScript.Length 1) + «`n`t)`n $deletionScript»

$DestinationConnectionString = «Data Source=$Destinationinstance;Initial Catalog=$Destinationdatabase;$(

    if ($DestinationWindowsSecurity) { ‘integrated security=true’ }

    else { ‘User Id=’ + $DestinationUserID + ‘;Password=’ + «$(((Get-Credential $DestinationUserID).GetNetworkCredential()).Password)» + ‘;integrated security=false’ }

try

{

    #test to see if the table is there. If it isn’t, then create it. If it is, then delete the contents

    $SqlCommand = new-object (‘Data.SqlClient.SqlCommand’) $CreateScript, $DestinationConnectionString;

    $SqlCommand.Connection.Open();

    $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] { param ($sender,

            $event) Write-Host «Message: $($event.Message)» };

    $SqlCommand.Connection.add_InfoMessage($handler);

    $success = $SqlCommand.ExecuteNonQuery();

    #now squirt the data in using the bulk copy library.

    $bulkCopy = new-object («Data.SqlClient.SqlBulkCopy») $DestinationConnectionString

    $bulkCopy.DestinationTableName = $DestinationTable

    $bulkCopy.BatchSize = 5000 #The number of rows in each batch sent to the server

    $bulkcopy.NotifyAfter = 200 #The number of rows to copy before firing a notification

    $bulkCopy.BulkCopyTimeout = 0 #the number of seconds before a time-out

    $objectEvent = Register-ObjectEvent $bulkcopy SqlRowsCopied -Action { write-host «Copied $($eventArgs.RowsCopied) rows « }

    $bulkCopy.WriteToServer($reader) #copy all rows to the server

}

catch

{

    $ex = $_.Exception

    Write-Error «Whilst doing the bulk copy ‘$($Query.CommandText)’ $ex.Message Sorry, but we can’t proceed because of this!»

}

$Reader.Close()

$SqlCommand.Connection.Close()

$Connection.Close()

OK, but does it work with real data? Off to the Health and Social Care Information Centre for some realistic data in spreadsheet form. I’ve included some data just so you don’t have to go to the site to play along, but it is far better to use the latest version of this data from the site. I’m sure I don’t have to tell you how easy this is to do in a script via PowerShell.

$ExcelFilePath=‘MyPathhosp-epis-stat-admi-tot-ops-11-12-tab.xlsx’ #the full path of the excel workbook

$Worksheet=‘Total procedures’ #this is the actual worksheet where the data is

$DataRange=  ‘A16:J1509’  #e.g. ‘A2:M33’  this is the range of the cells that make up the table. leave blank to read the whole worksheet

Also

$DestinationTable=‘Hosp’ # or whatever you want. The name of the SQL Server table where you want to put the data

 …and

$PrecisionForNumericData=0

 Try it. Whoosh. In it goes. If you were doing this as a routine, you’d be wanting to wrap this script into a function with parameters by now, but you know how to do this already, I’m sure. I’m trying to give you the ‘workbench’ narrative here.

Writing to Excel from SQL Server.

The process of going from SQL Server to excel via ODBC is, I think, needlessly complicated, especially if you use parameterised queries (excellent for SQL Server but they add very little for writing to Excel).In this example, I’ll do the old and horrible approach of using insert statements. There are other ways, including even using a dataset, but this is the most obvious.

I’m not particularly happy with this sample because Excel whines a bit when it opens it, saying that it is in the wrong format, (which it is, but you try naming it XLSX) but it deigns to open it.

“The file you are trying to open, ‘MyExcelFile.xls’, is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?”

More seriously, it complains that the numbers in the columns are ‘formatted as text’. It turns out that the data is saved in the correct format, but the next time the file is opened, all columns revert to varchar. Seasoned users of ODBC gets used to the bugs, but if anyone knows of a workaround to this, I’d be grateful.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

set-psdebug -strict

$ErrorActionPreference = «stop»

$Sourceinstance = ‘MyServerOrInstance’ #the name of the server or instance

$Sourcedatabase = ‘AdventureWorks’ #the name of the datatabase where you want to get the data

#here is where we put the SQL command to get the result from the database

$SelectStatementForDatabase =

SELECT ProductNumber, p.Name AS ProductName, color,  SafetyStockLevel, ReorderPoint, StandardCost, ListPrice,

NonDiscountSales = (OrderQty * UnitPrice), Discounts = ((OrderQty * UnitPrice) * UnitPriceDiscount)

FROM Production.Product AS p

INNER JOIN Sales.SalesOrderDetail AS sod

ON p.ProductID = sod.ProductID

where ((OrderQty * UnitPrice) * UnitPriceDiscount)>0

ORDER BY ProductName DESC;

«@

$SourceWindowsSecurity = $false #or $True if you are using Windows security

$SourceUserID = ‘SA’ #the name of the SQL Server user if not integrated security

$DestinationTable = ‘ProductWithDiscounts’

$DestinationExcelFilePath = ‘MyPathMyName.xls’ #the full path of the excel workbook

$DestinationHeader = $true # true if you want your first row to be read as column headers

#firstly, we create a connection string ‘on the fly’

#connect to the datanbase

#…and get the DataReader object

$SourceConnectionString = «Data Source=$Sourceinstance;Initial Catalog=$Sourcedatabase;$(

    if ($SourceWindowsSecurity) { ‘integrated security=true’ }

    else { ‘User Id=’ + $SourceUserID + ‘;Password=’ + «$(((Get-Credential $SourceUserID).GetNetworkCredential()).Password)» + ‘;integrated security=false’ })»

try

{

    #here we open a connection to the SQL Server source database

    $SqlCommand = new-object (‘Data.SqlClient.SqlCommand’)  $SelectStatementForDatabase, $SourceConnectionString;

    $SqlCommand.Connection.Open(); #we open the connection

    $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {

        param ($sender,

            $event) Write-Host «Message: $($event.Message)»

    };

    $SqlCommand.Connection.add_InfoMessage($handler);

    $Reader = $SqlCommand.ExecuteReader([System.Data.CommandBehavior]::SequentialAccess) #get the datareader and just get the result in one gulp

}

catch

{

    $ex = $_.Exception

    Write-Error «whilst getting data from  $Sourceinstance  $Sourcedatabase ‘ : $ex.Message»

    exit

}

# excel has only the LOGICAL,CURRENCY,NUMBER,VARCHAR,DATETIME datatypes

# according to $connection.GetSchema(‘DATATYPES’).TypeName

# lets work out what the Excel datatype would be…

$columns = $reader.GetSchemaTable() | select columnName, datatype, @{

    name = ‘ExcelDatatype’; expression = {

        switch ($_.datatype)

        { { @(‘float’, ‘decimal’, ‘Numeric’) -contains $_ } { ‘Number’ } ‘bit’ { ‘logical’ } ‘int16’{ ‘Int’ } { @(‘smallmoney’, ‘money’) -contains $_ } { ‘currency’ } ‘DateTime’{ ‘datetime’ }

            default { ‘VarChar’ }

        }

    }

}

# now we need to create an equivalent worksheet in the Workbook.

#If there is no workbook, it will create it

$CreateScript =

CREATE TABLE $DestinationTable (

«@

$CreateScript += $columns | foreach-object{ «`n`t$($_.ColumnName.Trim())  $($_.ExcelDataType),» }

$CreateScript = $CreateScript.Substring(0, $CreateScript.Length 1) + «`n`t)»

# and make a columnlist for the insert statement.

$columnList = ‘[‘ + $columns[0].ColumnName + ‘]’

for ($ii = 1; $ii -le $columns.Length 1; $ii++) { $params += ‘,?’; $columnList += ‘ ,[‘ + $columns[$ii].ColumnName + ‘]’ }

try

{

    #to open the destination workbook or create it if not exist

    $Connection = New-Object system.data.odbc.odbcconnection

    $TheConnectionString = ‘Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=’ + $DestinationExcelFilePath + ‘;Mode=ReadWrite;ReadOnly=false;Extended Properties=»HDR=’ + «$(if ($DestinationHeader) { ‘YES’ }

        else { ‘NO’ })» + ‘»‘

    $Connection.ConnectionString = $TheConnectionString

    $Connection.Open()

    $insertionCommand = $Connection.CreateCommand()

}

catch

{

    $ex = $_.Exception

    Write-Error «whilst opening connection to  $DestinationExcelFilePath using ‘$($TheConnectionString)’ : $ex.Message»

    exit

}

try

{

    #if the table doesn’t exist we create it.

    $CreateTableCommand = $Connection.CreateCommand()

    $CreateTableCommand.CommandText = $CreateScript

    if ($connection.GetSchema(‘TABLES’).Table_Name -notcontains $DestinationTable)

    { if ($CreateTableCommand.ExecuteNonQuery() -eq -1) { write-host «created table (worksheet) $DestinationTable» } }

}

catch

{

    $ex = $_.Exception

    Write-Error «couldn’t create table with command $CreateScript : $ex.Message»

    exit

}

$rows = 0

try

{

    #now we create each insert statement on the fly! Developers look away, please

    while ($Reader.Read())

    {

        $insertcommand = «INSERT INTO [$destinationTable» + ‘$] (‘ + «$columnList) VALUES(«

        for ($i = 0; $i -lt $Reader.FieldCount; $i++)

        {

            $insertcommand += «$(if ($i -eq 0) { » }

                else { ‘,’ }) $(if ($columns[$i].ExcelDataType -eq ‘VarChar’) { «‘$($reader.GetValue($i) -replace «‘«, «»«)'» }

                else { «$($reader.GetValue($i))» }) «

        }

        $insertioncommand.CommandText = $insertcommand + ‘)’

        $rows += $insertionCommand.ExecuteNonQuery()

    }

}

catch

{

    $ex = $_.Exception

    Write-Error «whilst writing to column $i of file  $DestinationExcelFilePath ‘ : $ex.Message»

}

#we report what we’ve done.

write-host «Wrote $rows rows of $($columns.count) columns to worksheet $destinationTable»

$Reader.Close()

$SqlCommand.Connection.Close()

$connection.Close()

CSV and Delimited ODBC Sources: Text AdventureWorks.

Although the ACE drivers are used more by people reading Excel files, I must emphasize that there are drivers for a number of other formats. It is pretty easy, for example, to turn a bunch of CSV files into a relational database. Just to prove it, I’ve created a CSV/Text version of AdventureWorks, together with its schema.ini. This was originally created in this article The TSQL of CSV: Comma-Delimited of Errors. With this text-based database, you can do a lot of the sample AdventureWorks SQL examples with only a minor modification.

Once you’ve installed the ACE drivers, you’ll can use a modified version of the routine I showed you or exploring the PUBS Excel database to play along.

All you have to do is to unzip Text Adventureworks into a new directory with the name of your database (AdventureWorks) and point your connection string at the directory by giving it the full path to the directory. I just altered two lines

#set the directory in which your database should go.  

$TextFilePath=‘MyPathToTheDirectoryTextAdventureWorks’ #the path to the database

… and

$Connection.ConnectionString=‘Driver={Microsoft Access Text Driver (*.txt, *.csv)};DBQ=’+$TextFilePath+»

Now you should be ready with your text-based relational database.

You can, of course, create tables and write to them using the INSERT statement.

create table [Log#csv] (MyInteger int,TheDate date TheMessage char(125))

…and do insert statements into it. You can SELECT INTO as well, which is new to me. I didn’t notice this in previous incarnations of this driver.

With CREATE statements, you can use ‘BIT, BYTE , LONGCHAR, CURRENCY, INTEGER, SMALLINT, REAL, FLOAT, CHAR or DATETIME

(Out of curiosity, the OLEDB driver allows  Long, Single, Double, Currency, DateTime , Bit, Byte, GUID, BigBinary, LongBinary, VarBinary, LongText, VarChar char and Decimal)

# You can list out the tables

$Connection.GetSchema(«tables»)|select table_name  

And the schema

$Connection.GetSchema(«columns»)|select tableName, ColumnName, cardinalPosition

 Here are a few of the SQL Statements that work

SELECT * into [gloves#csv]

FROM [Production_ProductModel#csv]

WHERE ProductModelID IN (3, 4)

 SELECT count(*) as [discounted]

 FROM [Production_Product#csv] AS p

 INNER JOIN [Sales_SalesOrderDetail#csv] AS sod

 ON p.ProductID = sod.ProductID

 where ((OrderQty * UnitPrice) * UnitPriceDiscount)>0

SELECT Name, ProductNumber, ListPrice AS Price

FROM [Production_Product#csv]

WHERE ProductLine = ‘R’

AND DaysToManufacture < 4

ORDER BY Name DESC

SELECT p1.ProductModelID

FROM [Production_Product#csv] AS p1

GROUP BY p1.ProductModelID

having p1.ProductModelID >100

SELECT p1.ProductModelID

FROM [Production_Product#csv] AS p1

GROUP BY p1.ProductModelID

HAVING MAX(p1.ListPrice) >= ALL

 (SELECT AVG(p2.ListPrice)

 FROM [Production_Product#csv] AS p2

 WHERE p1.ProductModelID = p2.ProductModelID)

SELECT top 50 SalesOrderID, SUM(LineTotal) AS SubTotal

FROM [Sales_SalesOrderDetail#csv]

GROUP BY SalesOrderID

ORDER BY SalesOrderID;

SELECT ProductModelID, Name

FROM [Production_ProductModel#csv]

WHERE ProductModelID IN (3, 4)

union all

SELECT ProductModelID, Name

FROM [Production_ProductModel#csv]

WHERE ProductModelID NOT IN (3, 4)

Conclusions

If only Microsoft put some energy into their whole range of ODBC drivers, including all the possible datastores that can be mapped to relational databases,  they’d be the obvious way of transferring data, and would put Microsoft in great shape for providing ‘big data’ solutions.. As it is, they are extraordinarily useful, but marred by  quirks and oddities.

For me, ODBC is the obvious way to  script  data from Excel or Access into SQL Server, for doing data imports.  

   Vlaloplo

26.05.22 — 15:43

Всем доброго времени суток! Оговорю сразу, с данным методом загрузки не разу не работал, возможно у кого то уже был опыт с данной технологией

Есть код загрузки файла:

Знач ПодключениеADODB = «MicrosoftJetOLEDB40»)

    
    Перем СonnectionString, ADODBConnection, ADODBRecordset, ТекстЗапроса;

    Перем КолвоКолонокExcel, Поле, Колонка, ИмяКолонки;

    Перем НоваяСтрока, НомерСтроки;

    //Перем ТаблицаРезультат;

    
    // Нумерация MS ADODB начинается с 1.

    
    // Переменная «СтрокаЗаголовка», не используется, т.к. HDR=YES, а не HDR=NO.

    //HDR = YES;

    // 1. Считывание заголовков колонок с 1-ой строки.

    // 2. Считываемые данные со 2-ой и последующих строк типизированы. Для варианта HDR=NO: считываемые данные — строка.

    
    // Строка соединения — определение драйвера, который будет использован для подключения к файлу EXCEL.

    Если ПодключениеADODB = «MicrosoftACEOLEDB12» Тогда

        
        // ACE.OLEDB.12.0 — Для использования данного подключения необходимо дополнительное ПО:

        // Microsoft Access Database Engine 2010 Redistributable 32/64 bit.

        СonnectionString = «Provider=Microsoft.ACE.OLEDB.12.0;Data Source= » + СокрЛП(ФайлEXCEL) + «;Extended Properties=»»Excel 12.0;HDR=NO;IMEX=1;»»»;

        
        // Еще один вариант.

        //СтрокаСоединения = «Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=» + СокрЛП(ФайлEXCEL) + «;»;

        
    Иначе

        
        // Jet.OLEDB.4.0 — Стандартное подключение, как правило, не требующее установки дополнительного ПО.

        // Рекомендуется установить последний Service Pack Windows.

        СonnectionString = «Provider=Microsoft.Jet.OLEDB.4.0;Data Source= »  + СокрЛП(ФайлEXCEL) + «;Extended Properties=»»Excel 8.0;HDR=NO;IMEX=1;»»»;

        
        // Еще один вариант.

        //СтрокаСоединения = «Driver={Microsoft Excel Driver (*.xls)};Dbq=» + СокрЛП(ФайлEXCEL) + «;»;

        
    КонецЕсли;

    
    Попытка

        // Инициализация основного объекта ADODB.Connection. Открытие соединения.

        ADODBConnection = Новый COMОбъект(«ADODB.Connection»);

        ADODBConnection.ConnectionString =  СonnectionString;

        ADODBConnection.Open();

        // Импирически определенный параметр для правильного определения количества строк листа.

        ADODBConnection.CursorLocation = 3;    // По-умолчанию 2.

    Исключение

        Сообщить(НСтр(«ru = ‘»+ОписаниеОшибки()+»‘»), СтатусСообщения.Внимание);

        //Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.

    КонецПопытки;

    
    ТекстЗапроса = «SELECT * FROM [» + ИмяЛиста + «$]»;

    
    // Создание Recordset. Дочерний объект ADODBConnection. Набор записей по запросу.

    Попытка

        ADODBRecordset = Новый COMОбъект(«ADODB.Recordset»);

        ADODBRecordset.Open(ТекстЗапроса, ADODBConnection);

        
        // Проверка заполненности листа.

        Если (ADODBRecordset.EOF ИЛИ ADODBRecordset.BOF) Тогда

            КолвоСтрокExcel = 0;

            Сообщить(НСтр(«ru = ‘» + ИмяЛиста + «: не содержит данных.'»), СтатусСообщения.Внимание);

            
            // Завершение работы.

            // Закрытие Объектов.

            ADODBRecordset.Close();

            ADODBConnection.Close();

            ADODBRecordset   = Неопределено;

            ADODBConnection = Неопределено;

            
            //Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.

        КонецЕсли;

        
        // Импирически определенные параметры для правильного определения количества строк листа.

        ADODBRecordset.AbsolutePage     = 1;

        ADODBRecordset.AbsolutePosition = 1;

    Исключение

        Сообщить(НСтр(«ru = ‘»+ОписаниеОшибки()+»‘»), СтатусСообщения.Внимание);

        //Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.

    КонецПопытки;

    
    // Параметр, возвращаемый в вызывающую процедуру.

    КолвоСтрокExcel = ADODBRecordset.RecordCount + 1;    // (+1) — учет Строки-Заголовока, которая «съедается».

    КолвоКолонокExcel = ADODBRecordset.Fields.Count;

    
    // Проверка заполненности листа.

    Если КолвоСтрокExcel <= 2 Тогда

        КолвоСтрокExcel = 0;

        Сообщить(НСтр(«ru = ‘» + ИмяЛиста + «: не содержит данных.'»), СтатусСообщения.Внимание);

        
        // Завершение работы.

        // Закрытие Объектов.

        ADODBRecordset.Close();

        ADODBConnection.Close();

        ADODBRecordset   = Неопределено;

        ADODBConnection = Неопределено;

        
        //Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.

    КонецЕсли;

    
    // Создание результирующей таблицы, в которую будут записываться считанные из EXCEL данные.

    //ТаблицаРезультат = Новый Массив;

    
    НомерСтроки = -1;

    
    ПаспортРФ = ПолучитьВидДокумента(«Паспорт гражданина РФ»);

    ПаспортИН = ПолучитьВидДокумента(«Иностранный паспорт»);

    
    Пока ADODBRecordset.EOF() = 0 Цикл

        
        НомерСтроки = НомерСтроки + 1;

        
        Если НомерСтроки = КолвоСтрокExcel — 1 Тогда

            ADODBRecordset.MoveNext();            

            Продолжить;

        КонецЕсли;

        
        Если НомерСтроки < НачСтрока Тогда    // Номер строки вне диапазона считываемых строк.

            ADODBRecordset.MoveNext();             // Следующая строка.

            Продолжить;

        КонецЕсли;

        
        Если КонСтрока > 0 И НомерСтроки > КонСтрока Тогда    // Номер строки вне диапазона считываемых строк.

            Прервать;

        КонецЕсли;

На строчке КолвоСтрокExcel = ADODBRecordset.RecordCount + 1; вылетает ошибка: «Произошла исключительная ситуация (ADODB.Recordset): Операция не допускается, если объект закрыт.». Работа проходит на сервере по rdp, но у некоторых пользователей (сервера) эта ошибка возникает, а у кого то нет. Загружается одна и та же excel. В чем предположительно может быть дело? Или может есть понимание, что за объект нужно открыть? пробовал открывать сам файл excel и загружать так, не сработало

   Vlaloplo

1 — 26.05.22 — 15:44

Данных код писал не я, поэтому сложно понять, что тут вообще происходит ..

   Vlaloplo

2 — 26.05.22 — 16:06

UP

   Kassern

3 — 26.05.22 — 16:10

Новый табличный документ вам в помощь) 1ска умеет без всяких адобов читать эксельки

   Kassern

4 — 26.05.22 — 16:11

*адодбов)

   Vlaloplo

5 — 26.05.22 — 16:14

(3) Да я тоже это это никогда не юзал) Но интерес, а так же лень переделки кода загрузки заново взяла вверх и завела сюда)

   sandal_r

6 — 26.05.22 — 16:17

а ошибка «%ИмяЛиста% не содержит данных» не вылазит?

   Vlaloplo

7 — 26.05.22 — 16:18

(6) нет, имя Листа это реквизит на форме, который заполнен

   Vlaloplo

8 — 26.05.22 — 16:19

Ну или я до неё не до хожу

   sandal_r

9 — 26.05.22 — 16:29

судя по империческим вставкам — код писался методом тыка.

поддержку (3), переписать будет быстрее, чем отловить баги (тем более не всегда повторяющиеся)

   Vlaloplo

10 — 26.05.22 — 17:21

Ещё раз UP, что то уже может под вечер туплю..

Пишу:

табДок = Новый ТабличныйДокумент;

табДок.Прочитать(ИмяФайла); (имя файла — полный путь к файлу с его названием, тип строка естественно)

На что получаю ошибку «Ошибка при выполнении файловой операции». Пробовал и на стороне сервера и на стороне клиента — однотипно. Думал может с файлом что то не то, пересоздал файл заново, сохранил и всё равно так

   Kassern

11 — 26.05.22 — 17:24

(10) файл случайно не открыт у вас? Файл на сервере 1с доступен? На клиенте вы его и не откроете. Через клиент вы можете получить двоичные данные файла и передать через временное хранилище на сервер. А там уже получить с хранилища и записать во временный файл на сервере. С ним уже работать

   Vlaloplo

12 — 26.05.22 — 17:26

(11),Файл закрыт. Как я могу проверить, что файл на сервере доступен?

   Kassern

13 — 26.05.22 — 17:29

(12) зайти на сам сервер и попробовать на нем открыть. Либо поместить на физический диск сервера файл и прописать путь к нему

   Kassern

14 — 26.05.22 — 17:31

опять же может служба 1с под системной записью запущена, тогда по сети вряд ли сможет 1ска на сервере получать файлы. Поэтому либо через клиент передавайте ДД, либо кидайте файлы на сервер, либо запускайте службу 1с с правами для папки где файлы лежат

   NorthWind

15 — 26.05.22 — 17:57

(0) у вас возврат из процедуры должен быть в том случае, если лист не заполнен. Но возврат этот закомменчен

     //Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значени

а в данной ветке перед возвратом происходит закрытие рекордсета. Соответственно исполнение идет дальше. Оно пытается сделать

КолвоСтрокExcel = ADODBRecordset.RecordCount + 1;

эта манипуляция требует открытого рекордсета, а он у вас ранее был закрыт. Вот и ошибка.

   NorthWind

16 — 26.05.22 — 17:58

в принципе, простейший код и откомментирован нормально

   тук-тук

17 — 26.05.22 — 20:11

(0) предлагаю заменить твою простынь на 8 строчек

ТабДок = Новый ТабличныйДокумент;

ТабДок.Прочитать(ПолноеИмяФайла, СпособЧтенияЗначенийТабличногоДокумента.Значение);

ПЗ = Новый ПостроительЗапроса;

ПЗ.ИсточникДанных = Новый ОписаниеИсточникаДанных(ТабДок.Область());

ПЗ.ДобавлениеПредставлений = ТипДобавленияПредставлений.НеДобавлять;

ПЗ.ЗаполнитьНастройки();

ПЗ.Выполнить();

ТЗ = ПЗ.Результат.Выгрузить();

   yopQua

18 — 26.05.22 — 20:22

ну по рекордсету инфы много, старый он уже, как и все кто с ним работал

когда то видел таблицу со всеми свойствами и методами и нормальным к ним описаниям.. на каком то желтом фоне было)

  

NorthWind

19 — 26.05.22 — 21:37

(18) MSDN

Excel 97

Standard (for versions 97 — 2003)

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

Excel 2000

Standard (for versions 97 — 2003)

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

Excel 2002

Standard (for versions 97 — 2003)

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

Excel 2003

Standard (for versions 97 — 2003)

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

Excel 2007

Standard

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

Excel 2010

Standard

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

Excel 2013

Standard

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

I have an excel file like this:

enter image description here

I have tow TextBox(Fname,Lname) , tow Button(Search,Insert) and one GridView to
show result.
Search button work fine but Insert button not work.

Error in insert Button is:

ERROR [HY000] [Microsoft][ODBC Excel Driver] Operation must use an updateable query.

ASPX:

Fname:<asp:TextBox ID="txtFname" runat="server"></asp:TextBox><br />
Lname:<asp:TextBox ID="txtLname" runat="server"></asp:TextBox><br />
<asp:Button ID="cmdSearch" runat="server" onclick="cmdSearch_Click" Text="Search" />
<asp:Button ID="cmdInsert" runat="server" onclick="cmdInsert_Click" Text="Insert" /><br />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>

Code behind:

string conStr = @"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};"+
    @"DBQ=|DataDirectory|q.xlsx;";
protected void cmdInsert_Click(object sender, EventArgs e)
{
    OdbcConnection con = new OdbcConnection(conStr);
    con.Open();
    string query = "insert into [Sheet1$] (Fname,Lname) values (?,?)";
    OdbcCommand cmd = new OdbcCommand(query, con);
    cmd.Parameters.AddWithValue("?",txtFname.Text);
    cmd.Parameters.AddWithValue("?", txtLname .Text);
    cmd.ExecuteNonQuery();// has error
    con.Close();
}
protected void cmdSearch_Click(object sender, EventArgs e)
{
    OdbcConnection con = new OdbcConnection(conStr);
    con.Open();
    string query = "";
    query = "select * from [Sheet1$] where Fname=? or Lname=?";
    OdbcCommand cmd = new OdbcCommand(query, con);
    cmd.Parameters.AddWithValue("?", txtFname.Text);
    cmd.Parameters.AddWithValue("?", txtLname.Text);
    GridView1.DataSource = cmd.ExecuteReader();
    GridView1.DataBind();
    con.Close();
}

ПРАКТИЧЕСКОЕ ПОСОБИЕ РАЗРАБОТЧИКА: Метод «MS ADODB» (ADODB.Connection).
Поддерживаемые типы: *.xls,*.xlsx.


 В данной статье приведен функционал, с помощью которого в обработке
«Импорт из EXCEL и др.источников (xls,xlsx,ods,sxc,dbf,mxl,csv,sql) в 1С»: //infostart.ru/public/120961
производится считывание данных из файлов табличного типа *xls, *.xlsx.

Методы загрузки из внешнего источника:
— Метод «MS ADO» (Чтение файлов xls, xlsx средствами Microsoft ADO): //infostart.ru/public/163640/
— Метод «MS EXCEL» (Чтение файлов xls, xlsx с картинками средствами Microsoft Office): //infostart.ru/public/163641/
— Метод «LO CALC» (Чтение файлов xls, xlsx, ods, sxc с картинками средствами LibreOffice): //infostart.ru/public/163642/
— Метод «NativeXLSX» (Чтение файлов xlsx с картинками средствами 1С. ПостроительDOM): //infostart.ru/public/300092/
— Метод «NativeXLSX». Предыдущий вариант (Чтение файлов xlsx средствами 1С. ЧтениеXML):
//infostart.ru/public/225624/
— Метод «Excel1C» (Загрузка на платформе 8.3.6 с картинками. Чтение файлов xls, xlsx, ods): //infostart.ru/public/341855/
— Список листов файла: //infostart.ru/public/163724/


Публикаций на тему загрузки из EXCEL — множество, но
«
— Вам билетёр нужен?
— Был нужен, да уже взяли.
— Может и я на что сгожусь?
— Может и сгодишься, если скалиться не будешь …
«
«НЕУЛОВИМЫЕ МСТИТЕЛИ» (1966).


// Метод "Microsoft ADODB"/
//

// Параметры:
//        ФайлEXCEL - Полное имя файла (путь к файлу с именем файла и расширением).
//        ИмяЛиста - Имя выбранного листа файла EXCEL.
//        СтрокаЗаголовка (по умолчанию = 1) - Номер строки EXCEL, в которой расположены заголовки колонок.
//            Не используется, т.к. в СтрокеСоединения указано HDR=YES, а не HDR=NO.
/ /       В обработке 1-я строка анализируется для сопоставления колонок EXCEL с реквизитами 1С (справочники, докуметны, регистры).
//        НачСтрока (по-умолчанию = 0) - Номер начальной строки, начиная с которой считываются данные из EXCEL.

//        КонСтрока (по-умолчанию = 0) - Номер конечной строки, которой заканчиваются считываемые данные из EXCEL.
//            Если НачСтрока=0 и КонСтрока=0, то считывается вся таблица, находящаяся на листе EXCEL.
//        КолвоСтрокExcel - Количество строк на листе "ИмяЛиста" EXCEL. Возвращается в вызываемую процедуру.
//        ПодключениеADODB - тип драйвера ADODB для подключения к EXCEL.
//
// Возвращаемые значения:
//         ТаблицаРезультат - Результат считывания с листа "ИмяЛиста" EXCEL.
//
Функция ЗагрузитьМетодом_MSADODB(Знач ФайлEXCEL, Знач ИмяЛиста, Знач СтрокаЗаголовка = 1, НачСтрока = 0, КонСтрока = 0, КолвоСтрокExcel,
Знач
ПодключениеADODB = "MicrosoftJetOLEDB40") Экспорт
    Перем СonnectionString, ADODBConnection, ADODBRecordset, ТекстЗапроса;
    Перем КолвоКолонокExcel, Поле, Колонка, ИмяКолонки;
    Перем НоваяСтрока, НомерСтроки;
    Перем ТаблицаРезультат;

        // Нумерация MS ADODB начинается с 1.

        // Переменная "СтрокаЗаголовка", не используется, т.к. HDR=YES, а не HDR=NO.
    // HDR=YES:
    // 1. Считывание заголовков колонок с 1-ой строки.
    // 2. Считываемые данные со 2-ой и последующих строк типизированы. Для варианта HDR=NO: считываемые данные - строка.

        // Строка соединения - определение драйвера, который будет использован для подключения к файлу EXCEL.
    Если ПодключениеADODB = "MicrosoftACEOLEDB12" Тогда

                // ACE.OLEDB.12.0 - Для использования данного подключения необходимо дополнительное ПО:
        // Microsoft Access Database Engine 2010 Redistributable 32/64 bit.
        СonnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + СокрЛП(ФайлEXCEL) + ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""";

                // Еще один вариант.
        //СтрокаСоединения = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=" + СокрЛП(ФайлEXCEL) + ";";

            Иначе

                // Jet.OLEDB.4.0 - Стандартное подключение, как правило, не требующее установки дополнительного ПО.
        // Рекомендуется установить последний Service Pack Windows.
        СonnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "  + СокрЛП(ФайлEXCEL) + ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""";

                // Еще один вариант.
        //СтрокаСоединения = "Driver={Microsoft Excel Driver (*.xls)};Dbq=" + СокрЛП(ФайлEXCEL) + ";";

            КонецЕсли;

        Попытка
        // Инициализация основного объекта ADODB.Connection. Открытие соединения.
        ADODBConnection = Новый COMОбъект("ADODB.Connection");
        ADODBConnection.ConnectionString =  СonnectionString;
        ADODBConnection.Open();
        // Импирически определенный параметр для правильного определения количества строк листа.
        ADODBConnection.CursorLocation = 3;    // По-умолчанию 2.
    Исключение
        Сообщить(НСтр("ru = '"+ОписаниеОшибки()+"'"), СтатусСообщения.Внимание);
        Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.
    КонецПопытки;

        ТекстЗапроса = "SELECT * FROM [" + ИмяЛиста + "$]";

        // Создание Recordset. Дочерний объект ADODBConnection. Набор записей по запросу.
    Попытка
        ADODBRecordset = Новый COMОбъект("ADODB.Recordset");
        ADODBRecordset.Open(ТекстЗапроса, ADODBConnection);

                // Проверка заполненности листа.
        Если (ADODBRecordset.EOF ИЛИ ADODBRecordset.BOF) Тогда
            КолвоСтрокExcel = 0;
            Сообщить(НСтр("ru = '" + ИмяЛиста + ": не содержит данных.'"), СтатусСообщения.Внимание);

                        // Завершение работы.
            // Закрытие Объектов.
            ADODBRecordset.Close();
            ADODBConnection.Close();
            ADODBRecordset   = Неопределено;
            ADODBConnection = Неопределено;

                        Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.
        КонецЕсли;

                // Импирически определенные параметры для правильного определения количества строк листа.
        ADODBRecordset.AbsolutePage     = 1;
        ADODBRecordset.AbsolutePosition = 1;
    Исключение
        Сообщить(НСтр("ru = '"+ОписаниеОшибки()+"'"), СтатусСообщения.Внимание);
        Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.
    КонецПопытки;

        // Параметр, возвращаемый в вызывающую процедуру.
    КолвоСтрокExcel = ADODBRecordset.RecordCount + 1;    // (+1) - учет Строки-Заголовока, которая "съедается".
    КолвоКолонокExcel = ADODBRecordset.Fields.Count;

        // Проверка заполненности листа.
     Если КолвоСтрокExcel <= 2

Тогда
        КолвоСтрокExcel = 0;
        Сообщить(НСтр("ru = '" + ИмяЛиста + ": не содержит данных.'"), СтатусСообщения.Внимание);
        
        // Завершение работы.
        // Закрытие Объектов.
        ADODBRecordset.Close();
        ADODBConnection.Close();
        ADODBRecordset   = Неопределено;
        ADODBConnection = Неопределено;
        
        Возврат Новый ТаблицаЗначений;    // В случае ошибки возвращаем пустую таблицу значений.
    КонецЕсли;
    
    // Создание результирующей таблицы, в которую будут записываться считанные из EXCEL данные.
    ТаблицаРезультат = Новый ТаблицаЗначений;
    
    // Формирование колонок результирующей таблицы.
    
    // "НомерСтроки" - для наглядности и удобства.
    // В зависимости от разрабатываемой обработки.
    // "Сопоставлено" - может быть другим.
    // Здесь же могут быть добавлены другие колонки, не формируемые из содержимого файла EXCEL.
    ТаблицаРезультат.Колонки.Добавить("НомерСтроки", Новый ОписаниеТипов("Число"), "№", 4);
    ТаблицаРезультат.Колонки.Добавить("Сопоставлено", Новый ОписаниеТипов("Булево"), "Сопоставлено", 1);
    Для ит = 1 ПО КолвоКолонокExcel Цикл
        
        Поле = ADODBRecordset.Fields.Item(ит - 1);
        ИмяКолонки = "К_" + ит;
        Колонка = ТаблицаРезультат.Колонки.Добавить(ИмяКолонки, , СокрЛП(СтрЗаменить(Поле.Name, "#", ".")));
        // Замена "#" на ".", т.к. при считывании ADODB "." в имени колонки заменяется на "#".
        
    КонецЦикла;
    
    // ТаблицаРезультат: 1-я строка - Строка-Заголовок.
    
    // Добавление этой строки обусловлено исключительно из соображений идентичности содержимого файла EXCEL и ТаблицыЗначений,
    // выводимой на форме Обработки, и дальнейшей обработки строки заголовка
    // с целью сопоставления колонок EXCEL и реквизитов 1С: для Справочников, ПВХ, Регистров, Документов.
    
    // Если в Вашей обработке в результирующей таблице в качестве 1-ой строки не нужна Строка-Заголовок, то
    // следует закомментировать следующий цикл:
    НоваяСтрока = ТаблицаРезультат.Добавить();
    НоваяСтрока.НомерСтроки = 1;
    Для ит = 1 ПО КолвоКолонокExcel Цикл
        
        ИмяКолонки = "К_" + ит;
        Колонка = ТаблицаРезультат.Колонки.Найти(ИмяКолонки);
        НоваяСтрока[ИмяКолонки] = Колонка.Заголовок;
        
    КонецЦикла;
    
    // ТаблицаРезультат: Формирование строк по указанному диапазону: НачСтрока - КонСтрока.
    
    НомерСтроки = 1;
    Пока ADODBRecordset.EOF() = 0 Цикл
        
        НомерСтроки = НомерСтроки + 1;
        
        Если НомерСтроки < НачСтрока Тогда    // Номер строки вне диапазона считываемых строк.
            ADODBRecordset.MoveNext();             // Следующая строка.
            Продолжить;
        КонецЕсли;
        
        Если КонСтрока > 0 И НомерСтроки > КонСтрока Тогда    // Номер строки вне диапазона считываемых строк.
            Прервать;
        КонецЕсли;
        
        НоваяСтрока = ТаблицаРезультат.Добавить();
        НоваяСтрока.НомерСтроки = НомерСтроки;
        
        Для ит = 1 ПО КолвоКолонокExcel Цикл
            
            Поле = ADODBRecordset.Fields.Item(ит - 1);
                
            Если Поле.ActualSize = 0 Тогда        // Пустое поле EXCEL.
                Продолжить;
            КонецЕсли;
                
            ЗначениеЯчейки = Поле.Value;        // Учитывая параметр HDR=YES в строке соединения, данные считываются в соответствии с их типом.
            
            ИмяКолонки = "К_" + ит;
            НоваяСтрока[ИмяКолонки] = ЗначениеЯчейки;
            
            // Используется при формировании таблицы на форме обработки.
            ШиринаКолонки = ТаблицаРезультат.Колонки[ИмяКолонки].Ширина;
            ДлинаСтроки      = СтрДлина(СокрЛП(ЗначениеЯчейки));
            ТаблицаРезультат.Колонки[ИмяКолонки].Ширина = ?(ШиринаКолонки < ДлинаСтроки, ДлинаСтроки, ШиринаКолонки);
                
        КонецЦикла;
        
        ADODBRecordset.MoveNext();   // Следующая строка.
        
    КонецЦикла;
    
    //УдалитьКолонкиСНулевойШириной(ТаблицаРезультат);
    
    // Завершение работы.
    // Закрытие Объектов.
    ADODBRecordset.Close();
    ADODBConnection.Close();
    ADODBRecordset   = Неопределено;
    ADODBConnection = Неопределено;
    
    Возврат ТаблицаРезультат;

    
КонецФункции

Преимущества:
1.«В общем случае» метод «Microsoft ADODB» работает и в файловом и в клиент-серверном варианте.
2. Самый быстрый из трех рассматриваемых.
Особенности и Ограничения:
1.Для функционирования метода «Microsoft ADODB» необходимо:

Драйвер подключения Provider=Microsoft.Jet.OLEDB.4.0:
- Установленный Microsoft MDAC, как правило специальная установка не требуется, достаточно последнего Service Pack-а операционной системы..
  Microsoft MDAC 2.8 SP1 10.05.2005: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=5793

Драйвер подключения Provider=Microsoft.ACE.OLEDB.12.0:
- Установленный Microsoft Access Database Engine 2010 Redistributable (16/12/2010) 32 и 64 - разрядные версии:
  Microsoft ADE 2010 16/12/2010:  http://www.microsoft.com/en-us/download/details.aspx?id=13255


2. 1-я строка файла EXCEL - строка, содержащая заголовки колонок. Можно изменить, изменив HDR=YES на HDR=NO + некоторые изменения в функции.

МА! С уважением к сообществу МА!

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;

Понравилась статья? Поделить с друзьями:
  • Driver excel sql server
  • Earth point excel to kml
  • Drive drove driven is driving are word forms
  • Earth is not enough the word came first challenging the skilful coloured stereotype
  • Drinking games word games