OLEDB, Writing Excel cell without leading apostrophe

后端 未结 8 1532
长情又很酷
长情又很酷 2020-12-19 07:51

I\'m writing to Excel file using OLEDB (C#). What I need is just RAW data format.

I\'ve noticed all cells (headers and values) are prefixed by apostrophe (\')

<
相关标签:
8条回答
  • 2020-12-19 08:24

    Could you just use the Excel DSN? It seems to be pretty ubiquitous. I don't know .NET, so take this with a grain of salt, but here's my connection string for an OLEDB Query straight from a stock table:

    "Provider=MSDASQL.1;Persist Security Info=True;Extended Properties
    =""DSN=Excel Files;DBQ=" & filePath & "\" & fileName &
    ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"""
    

    And I used this basic INSERT statement:

    INSERT INTO rngOutput VALUES (1, 'ABC', '$1.00', 1300)
    

    When I did this, I didn't have any apostrophes in my data range. I'm also using Excel 2007, and I see you're using Excel 8.0 as your driver?

    Hopefully that nudges you toward a solution!

    0 讨论(0)
  • 2020-12-19 08:25

    Check the resistry Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows

    This decides how many rows should be scanned before deciding the format for the column. Default is 8, and 0 will force ADO to scan all column values before choosing the appropriate data type.

    0 讨论(0)
  • 2020-12-19 08:29

    Insert some dummy values for the columns which has apostrophe attached in the template file. Say for example for Name column put something like this dummyname, and age column put 99. Instead of inserting a new row in the template just update the row (Update..... where Name = 'dummyname' and age =99).

    This has worked for me..

    Hope it works for you also!

    0 讨论(0)
  • 2020-12-19 08:32

    I know that when entering data into Excel, prefixing it with an apostrophe is an easy way to make it into a text field. Are you sure the data does not actually contain the apostrophe? If it's added to the data at entry time, your only option would be to catch them at import time and dealing with them in some custom code.

    0 讨论(0)
  • 2020-12-19 08:38

    Remove IMEX=1 from your connection string.

    http://www.connectionstrings.com/excel

    0 讨论(0)
  • 2020-12-19 08:40

    Try the following hack to resolve the issue. Modify the template as per the instructions

    1. In the first data row just below the header row. Format the columns in the required format.
    2. Enter some dummy values like space for characters, 0 for numeric values etc.
    3. Hide the first data row that has the dummy values and save the template

    Now run your insert script using ADO.net

    -Venkat Kolla

    0 讨论(0)
提交回复
热议问题