把Excel表中的数据导入数据库

  

【需求】
          在实现竞价网站时,需要把Excel表中的数据周期地有条件地导入到网站位表中。这里结合数据库作业实现。

【步骤】

1、生成一个Excel表
   工作表名称:website
   包含的列:spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl
 
   部分截图:
 

2、存储过程
 
--1、涉及到的表:WL_SitePosition(网站位表),WL_ComSite(竞价网站表),Excel表(需要物理路径)
--2、功能:若网站位表不存在记录,则从Excel表中导入;反之,把网站位表中的记录导入到竞价网站表,
--同时,清空网站位表,之后再把Excel表中的数据导入到表WL_SitePosition;


CREATE PROCEDURE wl_JOB_WebsiteAuction AS

Declare @err1 int
Declare @err2 int
Declare @err3 int
Declare @err4 int
Declare @err5 int
Declare @err6 int

SET @err1 = 0
SET @err2 = 0
SET @err3 = 0
SET @err4 = 0
SET @err5 = 0
SET @err6 = 0

SET NOCOUNT ON
--设置事务隔离级别
--SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
--OLE/DB provider  不支持隔离层
Begin Tran --开始事务

  --SET XACT_ABORT ON --

   if (select count(*) from WL_SitePosition)>0
 begin
  --把数据从网站位表中导入到竞价网站表  
  TRUNCATE TABLE WL_ComSite --清空竞价网站表
  SET @err1 = @@error
  
  --把数据从网站位表中导入到竞价网站表中
  insert into WL_ComSite(cSiteName,cSiteUrl,cSiteVDT,spID,buyerID)
  select siteName,siteUrl,DATEADD(day,7,spEAucDT) as cSiteVDT,spID,buyerID
  from WL_SitePosition order by spID
  SET @err2 = @@error

  TRUNCATE TABLE WL_SitePosition--清空网站位表
  SET @err3 = @@error
  
  --把数据从Excel表导入到网站位表中
  insert into WL_SitePosition(spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl)
  SELECT spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl
  FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="D:/work/我傲/竞拍设计/webSitePosition_Data.xls";Extended Properties="Excel 8.0";Persist Security Info=False')...[website$]
  SET @err4 = @@error
     
 end
   else
 begin
  
  
  --把数据从Excel表导入到网站位表中
  insert into WL_SitePosition(spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl)
  SELECT spID,spName,spIniPrice,spIncExtent,spNowPrice,spImage,spBidNum,spAucType,spBAucDT,spEAucDT,spStat,buyerID,siteName,siteUrl
  FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="D:/work/我傲/竞拍设计/webSitePosition_Data.xls";Extended Properties="Excel 8.0";Persist Security Info=False')...[website$]
  SET @err5 = @@error
  
  --把数据从网站位表中导入到竞价网站表中
  insert into WL_ComSite(cSiteName,cSiteUrl,cSiteVDT,spID,buyerID)
  select siteName,siteUrl,DATEADD(day,7,spEAucDT) as cSiteVDT,spID,buyerID
  from WL_SitePosition order by spID
  SET @err6 = @@error

 end

   if @err1=0 and @err2=0 and @err3=0 and @err4=0 and @err5=0 and @err6=0

 COMMIT TRAN
   else
 ROLLBACK TRAN

SET NOCOUNT OFF
GO

3、创建数据库作业的截图

   






注意:数据库服务器要启动 SQL Server代理

【注意】
             1、Excel表使用绝对路径
              2、工作表的名称要一致,这里使用的是website

 【关键词】
               异构数据操作,MS Sql,Excel,数据库存储过程,数据库作业

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Apache POI 库来解析 Excel 表中数据,并使用 JDBC 将数据导入数据库。是一个简单的 Java 代码示: 首先,确保你已经将 POI 和 JDBC 相关的 JAR 文件添加到你的项目。 ```java import java.FileInputStream; import java.sql; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImporter { public static void main(String[] args) { String excelFilePath = "path/to/your/excel/file.xlsx"; try (FileInputStream inputStream = new FileInputStream(excelFilePath); Workbook workbook = new XSSFWorkbook(inputStream)) { Sheet sheet = workbook.getSheetAt(0); int rowCount = 0; for (Row row : sheet) { if (rowCount == 0) { rowCount++; continue; } String column1 = row.getCell(0).getStringCellValue(); int column2 = (int) row.getCell(1).getNumericCellValue(); // 获取其他列的数据 // 将数据插入数据库 insertToDatabase(column1, column2); rowCount++; } System.out.println("数据导入成功!"); } catch (Exception e) { e.printStackTrace(); } } private static void insertToDatabase(String column1, int column2) { String url = "jdbc:mysql://localhost:3306/your_database"; String username = "your_username"; String password = "your_password"; try (Connection connection = DriverManager.getConnection(url, username, password)) { String sql = "INSERT INTO your_table (column1, column2) VALUES (?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, column1); statement.setInt(2, column2); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 在上面的示例,你需要将 `path/to/your/excel/file.xlsx` 替换为你实际的 Excel 文件路径。此外,你还需要根据你的数据库设置更新 `url`、`username`、`password`、`your_database` 和 `your_table`。 这个示例假设 Excel 表的第一行为标题,从第二行开始是数据。你可以根据需要调整代码以适应不同的表格结构。同时,你可以根据你的数据库表结构调整 `insertToDatabase()` 方法的 SQL 语句和参数设置。 请确保你已正确引入所需的库和驱动程序,并在执行代码之前设置好数据库连接信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值