zoukankan      html  css  js  c++  java
  • Excel导入数据到数据库(Sql2005 ,Access)

    1.在Sql2005创建对应的表"Roll"

    2.应用以下这段代码

    string execelConnectionStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
                
    using (OleDbConnection conn = new OleDbConnection(execelConnectionStr))
                
    {
                    OleDbCommand cmd 
    = new OleDbCommand("select * FROM [Sheet1$]", conn);
                    conn.Open();
                    
    using (DbDataReader dr = cmd.ExecuteReader())
                    
    {
                        
    string sqlConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True";
                        
    // Bulk Copy to SQL Server 
                        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                        
    {
                            bulkCopy.DestinationTableName 
    = "Roll";
                            bulkCopy.WriteToServer(dr);
                        }

                    }

                }

    3.Excel导入、导出数据到access,使用Com组件
        public static void ExcelImportDB()
            
    {
                OleDbConnection conExcel 
    = new OleDbConnection();
                
    try
                
    {
                    ApplicationClass access 
    = new ApplicationClass();

                    access.Visible 
    = false;
                    access.OpenCurrentDatabase(Settings.Default.DBPath, 
    true"");

                    OpenFileDialog openFile 
    = new OpenFileDialog();
                    openFile.Filter 
    = ("Excel 文件(*.xls)|*.xls");

                    
    if (openFile.ShowDialog() == DialogResult.OK)
                    
    {
                        access.DoCmd.TransferSpreadsheet(AcDataTransferType.acImport, AcSpreadSheetType.acSpreadsheetTypeExcel12, 
    "Intergral", openFile.FileName, truenullnull);

                        access.CloseCurrentDatabase();
                        access.DoCmd.Quit(AcQuitOption.acQuitSaveAll);

                        Marshal.ReleaseComObject(access);

                        access 
    = null;

                        System.Windows.Forms.MessageBox.Show(
    "导入数据成功""导入数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }

                
    catch (Exception ex)
                
    {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }

                
    finally
                
    {
                    conExcel.Close();
                }

            }


            
    public static void DBExportExcel()
            
    {
                
    try
                
    {
                    ApplicationClass access 
    = new ApplicationClass();

                    access.Visible 
    = false;
                    access.OpenCurrentDatabase(Settings.Default.DBPath, 
    false"");

                    SaveFileDialog saveFile 
    = new SaveFileDialog();
                    saveFile.Filter 
    = ("Excel 文件(*.xls)|*.xls");
                    
    if (saveFile.ShowDialog() == DialogResult.OK)
                    
    {
                        access.DoCmd.TransferSpreadsheet(AcDataTransferType.acExport, AcSpreadSheetType.acSpreadsheetTypeExcel9, 
    "Intergral", saveFile.FileName, truenullnull);

                        access.CloseCurrentDatabase();
                        access.DoCmd.Quit(AcQuitOption.acQuitSaveNone);

                        Marshal.ReleaseComObject(access);

                        access 
    = null;

                        MessageBox.Show(
    "导出数据成功""导出数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(ex.ToString());
                }

            }
  • 相关阅读:
    站点被百度惩处应从哪些方面分析
    SVN 权限配置具体说明
    关于数组的应用
    实现键值对存储(二)——以现有键值对存储为模型
    大数据Lambda架构
    关于c++primer的一个代码错误
    怎样解决Ubuntu发热严重地问题
    【maven】pom.xml文件没错,但是项目有小红叉,Problems中可以看到错误:“Dynamic Web Module 3.0 requires Java 1.6 or newer.”
    【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面
    【shiro】使用shiro搭建的项目,页面引用js,报错:Uncaught SyntaxError: Unexpected token <
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1214418.html
Copyright © 2011-2022 走看看