Excle通过java与数据库交互(导入、导出。java例)

首先创建一个对应数据的实体,比如本例中使用到的事人物信息,则创建一个Peo类,包含id/name/account 等属性,不详细介绍,主要在于实现导入导出的方法代码:

具体要看都使用了那些类以及调用的方法,方法的作用。

从Excel中导出:

import java.io.File;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class FromExcle {
	/**
	 * 根据Excel文件路径导入数据
	 * @param path 文件路径
	 * @return 数据类型列表
	 */
	public static List<Peo> getAllByExcel(String path){
		List<Peo> lp = new ArrayList<Peo>();
		
		try {
			Workbook wb = Workbook.getWorkbook(new File(path));
			/* 找到 第1个sheet|| getSheet("sheet 1")*/
			Sheet rs = wb.getSheet(0);
			int clos = rs.getColumns();//获得所有的列
			int rows = rs.getRows();//获得所有的行
			System.out.println("列数:"+clos+"行数:"+rows);
			for (int i = 1; i < rows-1; i++) {
				for (int j = 0; j < clos; j++) {
					//获得第一列第二行数据 |默认最左侧编号也是一列
					String id = rs.getCell(j++, i+1).getContents();
					String name = rs.getCell(j++, i+1).getContents();
					String sex = rs.getCell(j++, i+1).getContents();
					String account = rs.getCell(j++, i+1).getContents();
					System.out.println("第"+i+"行数据为 -> "+"id:"+id+"name:"+name+"sex:"+sex+"account:"+account);
					lp.add(new Peo(Integer.parseInt(id), name, sex, account) );
				}
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lp;
	}
	
	public static void main(String[] args) {
		String filepath="C://Users//sinosoft//Desktop//20180131_163935_phone.xls";
		List<Peo> lp = FromExcle.getAllByExcel(filepath);
		for (Peo peo : lp) {
			System.out.println(peo.toString());
		}
	}
}

导入Excel中:

注:这里的导入要有规定的模板

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ToExcle {
	
	/**
	 * 将数据表导入的Excel表格中
	 * @param listp 实体线性表
	 */
	public void readData(List<Peo> listp){
		
		WritableWorkbook wwb = null;
		Date d = new Date();
		SimpleDateFormat sfm = new SimpleDateFormat("yyyyMMdd_HHmmss");
		//获取当前系统桌面路径
		File dir = FileSystemView.getFileSystemView().getHomeDirectory();
		//文件路径名
		File file = new File( dir+"\\"+"_phone.xls"+sfm.format(d));
		System.out.println("文件存储在: "+file.getAbsolutePath());
		
		try {
			if(!file.exists())file.createNewFile();
			
			//以filePath为文件名创建一个Workbook
			wwb = Workbook.createWorkbook(file);
			//创建工作表
			WritableSheet ws = wwb.createSheet("Test", 0);
			//通过查询到的list
			List<Peo> list = listp;
			//要插入到的Excel表格的行号,默认从0开始(列,行,内容)
			ws.addCell(new Label(2,0,"信息表"));
			ws.addCell(new Label(0,1,"编号(id)"));
			ws.addCell(new Label(1,1,"姓名"));
			ws.addCell(new Label(2,1,"性别"));
			ws.addCell(new Label(3,1,"账号"));
			for(int i = 0 ; i<list.size(); i++){
				//插入数据
				ws.addCell(new Label(0,i+2,list.get(i).getId().toString()));
				ws.addCell(new Label(1,i+2,list.get(i).getName()));
				ws.addCell(new Label(2,i+2,list.get(i).getSex()));
				ws.addCell(new Label(3,i+2,list.get(i).getAccount()));
			}
			//写进文档
			wwb.write();
			//关闭Excel工作对象
			wwb.close();
		} catch (IOException e) {
			System.out.println("IOException");
			e.printStackTrace();
		} catch (RowsExceededException e) {
			System.out.println("RowsExceededException");
			e.printStackTrace();
		} catch (WriteException e) {
			System.out.println("WriteException");
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		List<Peo> listp = new ArrayList<Peo>();
		listp.add(new Peo(1,"hah","男","按时打发第三方"));
		listp.add(new Peo(2,"dads","女","2432355232"));
		listp.add(new Peo(3,"adf","男","yiyu456"));
		listp.add(new Peo(4,"ddd","女","yrtyrt435"));
		listp.add(new Peo(5,"ggg","女","fhfgj534方"));
		ToExcle te = new ToExcle();
		te.readData(listp);
	}

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值