zoukankan      html  css  js  c++  java
  • 几篇操作Excel的文章资源

    http://www.connectionstrings.com/excel

    1、Connection strings for Excel

    Providers to use when connecting to Excel

    » Microsoft Jet OLE DB 4.0
    » .NET Framework Data Provider for OLE DB (OleDbConnection)
    » Microsoft Excel ODBC Driver
    » .NET Framework Data Provider for ODBC (OdbcConnection)
    » .NET xlReader for Microsoft Excel (ExcelConnection)

    Finding this site useful?
    -Say "Thanks" to Paul Meysembourg and the other contributing developers!
    -Thank you! »

    Microsoft Jet OLE DB 4.0

    Type:    OLE DB Provider
    Usage:  Provider=Microsoft.Jet.OLEDB.4.0

    Manufacturer:  Microsoft

    Customize string example values »

    Standard

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

    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";

    Important note!
    The quota " in the string needs to be escaped using your language specific escape syntax.
    c#, c++   \"
    VB6, VBScript   ""
    xml (web.config etc)   "
    or maybe use a single quota '.
    "HDR=Yes;" indicates that the first row contains columnnames, 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 * FROM [sheet1$]". I.e. excel worksheet name followed by a "$" and wrapped in "[" "]" brackets.
    Check out the [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel] 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.
    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."

    A workaround for the "could not decrypt file" problem

    .NET Framework Data Provider for OLE DB

    Type:    .NET Framework Wrapper Class Library
    Usage:  System.Data.OleDb.OleDbConnection

    Manufacturer:  Microsoft
    More info about this wrapper class library »

    Customize string example values »

    Bridging to Jet OLE DB 4.0

    This is just one connection string sample for the wrapping OleDbConnection class that calls the underlying OLEDB provider. See respective OLE DB provider for more connection strings to use with this class.

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

    Microsoft Excel ODBC Driver

    Type:    ODBC Driver
    Usage:  Driver={Microsoft Excel Driver (*.xls)}

    Manufacturer:  Microsoft

    Customize string example values »

    Standard

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

    SQL syntax "SELECT * FROM [sheet1$]". I.e. excel worksheet name followed by a "$" and wrapped in "[" "]" brackets.

    .NET Framework Data Provider for ODBC

    Type:    .NET Framework Wrapper Class Library
    Usage:  System.Data.Odbc.OdbcConnection

    Manufacturer:  Microsoft
    More info about this wrapper class library »

    Customize string example values »

    Bridging to Microsoft Excel ODBC Driver

    This is just one connection string sample for the wrapping OdbcConnection class that calls the underlying ODBC Driver. See respective ODBC driver for more connection strings to use with this class.

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

    .NET xlReader for Microsoft Excel

    Type:    .NET Framework Class Library
    Usage:  VM.xPort.ExcelClient.ExcelConnection

    Manufacturer:  xPortTools
    More info about this class library »

    Customize string example values »

    Excel file with header row

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

    Excel file without header row

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

    image

    image

    image

     

    http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx

    2、Reading and Writing Excel Spreadsheets Using ADO.NET C# DbProviderFactory

    by David Hayden ( Florida ASP.NET C# Developer )

    It has been an absolute exhausting work week. My eyes, wrists, and back are sore from too many hours of coding. Thankfully I get to relax much of the Memorial Day Weekend :) I hope you all enjoy the weekend, too.

    This week a client sent me an Excel Spreadsheet that needed to populate several tables in a SQL Server Database. To know me knows I hate data entry of any kind and there was no chance I was entering the Excel data in manually.

    Thankfully, we don't have to. You can use the OleDb Managed Data Providerin the .NET Framework to read an Excel Spreadsheet using ADO.NET and C#just like you would with a database.

    Shown below is a simple spreadsheet that lists a few cities ( Bradenton, Sarasota, and Tampa ) in Florida. Notice I have renamed the worksheet toCities as opposed to the default of Sheet1. Also notice that the first row contains headers of the columns. These changes will impact the way we access the information as you will see in a moment.

    David Hayden - Excel and ADO.NET

    Excel Connection String for ADO.NET

    You will first need a connection string to connect to the Excel Workbook, which would be the following:

    string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
    Data Source=Book1.xls;Extended Properties=
    ""Excel 8.0;HDR=YES;""";

    This says the spreadsheet is located in the current directory and called Book1.xls, and the first row is a header row containing the names of the columns.

    Read Excel Spreadsheet using ADO.NET and DbDataReader

    Once you have the connection string all normal ADO.NET coding applies. Here is some sample code that reads each row of the excel worksheet usingDbDataReader. You don't have to use the DbProviderFactory Classes. I thought I would show it just for kicks.

    string connectionString = @"Provider=Microsoft.Jet.
    OLEDB.4.0;Data Source=Book1.xls;Extended
    Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory =
    DbProviderFactories.GetFactory("System.Data.OleDb"); using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString; using (DbCommand command = connection.CreateCommand()) { // Cities$ comes from the name of the worksheet command.CommandText = "SELECT ID,City,State
    FROM [Cities$]"; connection.Open(); using (DbDataReader dr = command.ExecuteReader()) { while (dr.Read()) { Debug.WriteLine(dr["ID"].ToString()); } } } }

    Read Excel Spreadsheet using ADO.NET and DataSet

    Here is another example of reading an Excel spreadsheet using ADO.NET and a DataSet.

    string connectionString = @"Provider=Microsoft.Jet.
    OLEDB.4.0;Data Source=Book1.xls;Extended
    Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory =
    DbProviderFactories.GetFactory("System.Data.OleDb"); DbDataAdapter adapter = factory.CreateDataAdapter(); DbCommand selectCommand = factory.CreateCommand(); selectCommand.CommandText = "SELECT ID,City,State
    FROM [Cities$]"; DbConnection connection = factory.CreateConnection(); connection.ConnectionString = connectionString; selectCommand.Connection = connection; adapter.SelectCommand = selectCommand; DataSet cities = new DataSet(); adapter.Fill(cities); gridEX1.SetDataBinding(cities.Tables[0], ""); gridEX1.RetrieveStructure();

    I was binding to the Janus GridEx Control, which is why you see gridEX1 above. You could easily replace those 2 lines with

    dataGridView1.DataSource = cities.Tables[0].DefaultView;

    Inserting a Row into Excel Using ADO.NET

    Here I will add a 4th city, Tampa, to the list of cities in Florida. This inserts it right into the Excel Worksheet as you would expect.

    string connectionString = @"Provider=Microsoft.Jet.
    OLEDB.4.0;Data Source=Book1.xls;Extended
    Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory =
    DbProviderFactories.GetFactory("System.Data.OleDb"); using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString; using (DbCommand command = connection.CreateCommand()) { command.CommandText = "INSERT INTO [Cities$]
    (ID, City, State) VALUES(4,\"Tampa\",\"Florida\")"; connection.Open(); command.ExecuteNonQuery(); } }

    Updating Excel Using ADO.NET

    Let's modify the name of the first city from Bradenton to Venice in the Excel Spreadsheet using ADO.NET:

    string connectionString = @"Provider=Microsoft.Jet.
    OLEDB.4.0;Data Source=Book1.xls;Extended
    Properties=""Excel 8.0;HDR=YES;"""; DbProviderFactory factory =
    DbProviderFactories.GetFactory("System.Data.OleDb"); using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString; using (DbCommand command = connection.CreateCommand()) { command.CommandText = "Update [Cities$] Set City =
    \"Venice\" WHERE ID = 1"; connection.Open(); command.ExecuteNonQuery(); } }

    Conclusion

    It is just too cool that we can use ADO.NET and the OleDb Managed Data Provider in the .NET Framework to insert, update, and delete information in anExcel Spreadsheet like it was a database. View my other ADO.NET Tutorials.

    Have a great Memorial Day weekend!

    Source: David Hayden ( Florida ASP.NET C# Developer )

    Filed: ADO.NET 2.0, C#

  • 相关阅读:
    Oracle SGA详解
    oracle如何保证读一致性 第一弹
    Make命令完全详解教程
    ProC第三弹
    ProC第一弹
    ProC第二弹
    $(MAKE) , make命令
    转:跟我一起写Makefile (PDF重制版)
    [bzoj1105][POI2007]石头花园SKA
    可并堆学习
  • 原文地址:https://www.cnblogs.com/flyingfish/p/1629353.html
Copyright © 2011-2022 走看看