• 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());
                }

            }
  • 相关阅读:
    水晶报表基础操作技巧收藏
    注册表里相应的
    .Net 中的反射(动态创建类型实例) Part.4动态创建对象
    .Net 中的反射(查看基本类型信息) Part.2
    Javascript多级菜单
    诡异的中毒现象
    js魔力代码
    Silverlight入门教程
    Silverlight 2.0中文学习资源集萃
    jatoolsPrinter 2手册
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1214418.html
Copyright © 2020-2023  润新知