导入Excel格式保存到数据库

package com.jwy.excel;

import java.io.FileInputStream;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


/**
* 读取Excel文档内容
*
* @author Jingweiyu
*/
public class ReadExcel {
public static void main(String[] args) throws Exception {
// 创建文件输入流对象
FileInputStream is = new FileInputStream("readExcel.xls");
// 创建 POI文件系统对象
POIFSFileSystem ts = new POIFSFileSystem(is);
// 获取文档对象
HSSFWorkbook wb = new HSSFWorkbook(ts);
// 获取工作薄
HSSFSheet sheet = wb.getSheetAt(0);
// 声明行对象
HSSFRow row = null;
// 通过循环获取每一行

int totalRows = sheet.getLastRowNum();

System.out.println("输出总行数:" + totalRows);

for (int i = 2; sheet.getRow(i) != null; i++) {

row = sheet.getRow(i);

Users users= getEntity(row);
save(users);

HSSFCell idCell = row.getCell(0);

// HSSFCell accountCell = row.getCell(1);
// HSSFCell departmentCell = row.getCell(2);

//System.out.println("userNameCell=="
// + userNameCell.getRichStringCellValue());

//System.out.println("accountCell==" + accountCell);

//System.out.println("departmentCell==" + departmentCell);
// 循环获取一行的中列
for (int j = 0; row.getCell(j) != null; j++) {

// System.out.print(row.getCell(j).toString()+" ");

}

System.out.println();
}

}

public static Users getEntity(HSSFRow row) {

Users us = new Users();

us.setUserid(Integer.parseInt(row.getCell(0).getRichStringCellValue().toString()));
us.setUsername(row.getCell(1).toString());
us.setUserpwd(row.getCell(4).toString());

return us;

}


public static void save(Users users){



System.out.println("保存第次");
System.out.println( users.getUserid());
System.out.println( "name---"+users.getUsername());
System.out.println( "pwd----"+users.getUserpwd());



}



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在CSDN上,我可以分享一段Winform导入Excel保存数据库的源码。首先,我们需要创建一个Winform窗体,然后添加一个按钮用于触发导入Excel的操作。接着,我们可以编写代码来实现Excel导入操作,具体步骤如下: 1. 引用所需的命名空间: ```csharp using System.Data; using System.Data.OleDb; using System.Windows.Forms; ``` 2. 在按钮的点击事件中编写Excel导入的代码: ```csharp private void btnImport_Click(object sender, EventArgs e) { // 打开文件选择窗口,选择要导入Excel文件 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Excel文件|*.xls;*.xlsx"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; // 构建Excel连接字符串 string connStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';", filePath); // 使用OleDb连接到Excel文件 using (OleDbConnection conn = new OleDbConnection(connStr)) { conn.Open(); DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (dt != null && dt.Rows.Count > 0) { string sheetName = dt.Rows[0]["TABLE_NAME"].ToString(); // 从Excel中读取数据 string sql = string.Format("SELECT * FROM [{0}]", sheetName); OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn); DataTable data = new DataTable(); adapter.Fill(data); // 将数据保存数据库 // 这里可以编写将DataTable中的数据保存数据库的代码 } } } } ``` 3. 编写保存数据库的代码: 根据自己的实际情况,可以使用ADO.NET或者Entity Framework等方法将DataTable中的数据保存数据库。 以上就是简单的Winform导入Excel保存数据库的代码,希望对你有所帮助。如果需要完整的代码示例,可以在CSDN上搜索相关文章或者向我索取。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值