import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import jxl.*;
public class SimUpdate {
private String fileName;
public ZfzSimUpdate(String fileName){
this.fileName = fileName;
}
static Map tNames;
static{
tNames = new HashMap();
}
/**
* 用于产生 数据库的 ID 值,组成 [年月日时分秒(100-999)] 总共 17 位数.
* 根据不同的表名,可保证同一秒内产生的 ID 号不重复
*/
private static String getDtime() {
String rid;
Date nd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
rid = sdf.format(nd);
return rid;
}
public String getSeqNumber(String tableName) {
if(tableName == null || "".equals(tableName))
tableName = "GENERY";
Integer it;
// noinspection SynchronizeOnNonFinalField
synchronized(tNames){
it = (Integer)tNames.get(tableName);
if(it == null){
it = new Integer(100);
tNames.put(tableName, it);
}else{
if(it.intValue() > 998)
it = new Integer(100);
else
it = new Integer(1 + it.intValue());
tNames.put(tableName, it);
}
}
return getDtime() + String.valueOf(it);
}
private void updateDb(){
try{
Connection conn = DbPool.connectDB();
if(conn != null){
Statement stmt = conn.createStatement();
/**********************************************/
jxl.Workbook rwb = null;
try{
//构建Workbook对象 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
InputStream is = new FileInputStream(fileName);
rwb = Workbook.getWorkbook(is);
//Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下标是从0开始的
//获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
//获取Sheet表中所包含的总列数
int rsColumns = rs.getColumns();
//获取Sheet表中所包含的总行数
int rsRows = rs.getRows();
//获取指这下单元格的对象引用
String simNumber = "",termSeqId = "";
//指定SIM卡号及序列号
for(int i=0;ifor(int j=0;j Cell cell = rs.getCell(j,i);
if(j==0){
simNumber = cell.getContents();
}
termSeqId = "633"+simNumber;
}
String sql = "查询SQL";
int isOk = stmt.executeUpdate(sql);
if(isOk == 0 && !simNumber.equals("")){
String termId = getSeqNumber("termInf");
String insertSql = "自定义INSERT";
int isAdd = stmt.executeUpdate(insertSql);
if(isAdd > 0){
System.out.println("成功插入第"+i+"条数据");
}
}
//System.out.println("SIM卡号:"+simNumber+",序列号:"+termSeqId);
}
//以下代码为写入新的EXCEL,这里不使用,所以注释
/*
//利用已经创建的Excel工作薄创建新的可写入的Excel工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("D://Book2.xls"),rwb);
//读取第一张工作表
jxl.write.WritableSheet ws = wwb.getSheet(0);
//获取第一个单元格对象
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
//决断单元格的类型,做出相应的转化
if (wc.getType() == CellType.LABEL) {
Label l = (Label) wc;
l.setString("The value has been modified.");
}
//写入Excel对象
wwb.write();
wwb.close();
*/
}catch(Exception e){
e.printStackTrace();
}
finally{
//操作完成时,关闭对象,翻译占用的内存空间
rwb.close();
}
/*********************************************/
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
DbPool dbPool = new DbPool("dbConn.cfg");//连接数据库
SimUpdate simUpdate = new SimUpdate("zfz_sim.xls");
simUpdate.updateDb();
}
}
我只用了读取XLS,写入没试,应该没问题吧,你把注释了的拿 来试一下吧
一般使用CSV格式。
读取数据
生成CSV文件
下载CSV文件
第一步:如何用POI操作Excel
@Test
public void createXls() throws Exception{
//声明一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//声明表
HSSFSheet sheet = wb.createSheet("第一个表");
//声明行
HSSFRow row = sheet.createRow(7);
//声明列
HSSFCell cel = row.createCell(3);
//写入数据
cel.setCellValue("你也好");
这个基本上不需要 java 就能做了
在 excel 里面直接建一个数据源就行了
另外,也可以用 etl 的工具,比如 kettle 之类的
完整举例如下:
import java.io.*;
import java.sql.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class ExcelFile {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
PreparedStatement psmnt = null;
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("Select * from student");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Roll No");
rowhead.createCell((short) 1).setCellValue("Name");
rowhead.createCell((short) 2).setCellValue("Class");
rowhead.createCell((short) 3).setCellValue("Marks");
rowhead.createCell((short) 4).setCellValue("Grade");
int index = 1;
while (rs.next()) {
HSSFRow row = sheet.createRow((short) index);
row.createCell((short) 0).setCellValue(rs.getInt(1));
row.createCell((short) 1).setCellValue(rs.getString(2));
row.createCell((short) 2).setCellValue(rs.getString(3));
row.createCell((short) 3).setCellValue(rs.getInt(4));
row.createCell((short) 4).setCellValue(rs.getString(5));
index++;
}
FileOutputStream fileOut = new FileOutputStream("c:\\excelFile.xls");
wb.write(fileOut);
fileOut.close();
System.out.println("Data is saved in excel file.");
rs.close();
connection.close();
} catch (Exception e) {
}
}
}