import javaio;
import javatextSimpleDateFormat;
import javautil;
import javasql;
import javautilDate;
import javautilHashMap;
import javautilMap;
import jxl;
public class SimUpdate {
private String fileName;
public ZfzSimUpdate(String fileName){
thisfileName = 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 = sdfformat(nd);
return rid;
}
public String getSeqNumber(String tableName) {
if(tableName == null || ""equals(tableName))
tableName = "GENERY";
Integer it;
// noinspection SynchronizeOnNonFinalField
synchronized(tNames){
it = (Integer)tNamesget(tableName);
if(it == null){
it = new Integer(100);
tNamesput(tableName, it);
}else{
if(itintValue() > 998)
it = new Integer(100);
else
it = new Integer(1 + itintValue());
tNamesput(tableName, it);
}
}
return getDtime() + StringvalueOf(it);
}
private void updateDb(){
try{
Connection conn = DbPoolconnectDB();
if(conn != null){
Statement stmt = conncreateStatement();
//
jxlWorkbook rwb = null;
try{
//构建Workbook对象 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
InputStream is = new FileInputStream(fileName);
rwb = WorkbookgetWorkbook(is);
//Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下标是从0开始的
//获取第一张Sheet表
Sheet rs = rwbgetSheet(0);
//获取Sheet表中所包含的总列数
int rsColumns = rsgetColumns();
//获取Sheet表中所包含的总行数
int rsRows = rsgetRows();
//获取指这下单元格的对象引用
String simNumber = "",termSeqId = "";
//指定SIM卡号及序列号
for(int i=0;i<rsRows;i++){
for(int j=0;j<rsColumns;j++){
Cell cell = rsgetCell(j,i);
if(j==0){
simNumber = cellgetContents();
}
termSeqId = "633"+simNumber;
}
String sql = "查询SQL";
int isOk = stmtexecuteUpdate(sql);
if(isOk == 0 && !simNumberequals("")){
String termId = getSeqNumber("termInf");
String insertSql = "自定义INSERT";
int isAdd = stmtexecuteUpdate(insertSql);
if(isAdd > 0){
Systemoutprintln("成功插入第"+i+"条数据");
}
}
//Systemoutprintln("SIM卡号:"+simNumber+",序列号:"+termSeqId);
}
//以下代码为写入新的EXCEL,这里不使用,所以注释
/
//利用已经创建的Excel工作薄创建新的可写入的Excel工作薄
jxlwriteWritableWorkbook wwb = WorkbookcreateWorkbook(new File("D://Book2xls"),rwb);
//读取第一张工作表
jxlwriteWritableSheet ws = wwbgetSheet(0);
//获取第一个单元格对象
jxlwriteWritableCell wc = wsgetWritableCell(0, 0);
//决断单元格的类型,做出相应的转化
if (wcgetType() == CellTypeLABEL) {
Label l = (Label) wc;
lsetString("The value has been modified");
}
//写入Excel对象
wwbwrite();
wwbclose();
/
}catch(Exception e){
eprintStackTrace();
}
finally{
// *** 作完成时,关闭对象,翻译占用的内存空间
rwbclose();
}
//
}
}catch(Exception e){
eprintStackTrace();
}
}
public static void main(String args[]){
DbPool dbPool = new DbPool("dbConncfg");//连接数据库
SimUpdate simUpdate = new SimUpdate("zfz_simxls");
simUpdateupdateDb();
}
}
我只用了读取XLS,写入没试,应该没问题吧,你把注释了的拿 来试一下吧
Java *** 作Excel首先下载jar包 jxljar 下载地址: >
java 读excel 还是比较方便简单的,原理就是,先用java 读取excel,然后,一行行的写入数据库,字段的话,你自己程序里面写就行了,给你个例子:
从Excel读取数据,生成新的Excel,以及修改Excel
package commonutil;
import jxl;
import jxlformatUnderlineStyle;
import jxlwrite;
import jxlwriteNumber;
import jxlwriteBoolean;
import javaio;
/
Created by IntelliJ IDEA
User: xl
Date: 2005-7-17
Time: 9:33:22
To change this template use File | Settings | File Templates
/
public class ExcelHandle
{
public ExcelHandle()
{
}
/
读取Excel
@param filePath
/
public static void readExcel(String filePath)
{
try
{
InputStream is = new FileInputStream(filePath);
Workbook rwb = WorkbookgetWorkbook(is);
//Sheet st = rwbgetSheet("0")这里有两种方法获取sheet表,1为名字,而为下标,从0开始
Sheet st = rwbgetSheet("original");
Cell c00 = stgetCell(0,0);
//通用的获取cell值的方式,返回字符串
String strc00 = c00getContents();
//获得cell具体类型值的方式
if(c00getType() == CellTypeLABEL)
{
LabelCell labelc00 = (LabelCell)c00;
strc00 = labelc00getString();
}
//输出
Systemoutprintln(strc00);
//关闭
rwbclose();
}
catch(Exception e)
{
eprintStackTrace();
}
}
/
输出Excel
@param os
/
public static void writeExcel(OutputStream os)
{
try
{
/
只能通过API提供的工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数,
因为类WritableWorkbook的构造函数为protected类型
method(1)直接从目标文件中读取WritableWorkbook wwb = WorkbookcreateWorkbook(new File(targetfile));
method(2)如下实例所示 将WritableWorkbook直接写入到输出流
/
WritableWorkbook wwb = WorkbookcreateWorkbook(os);
//创建Excel工作表 指定名称和位置
WritableSheet ws = wwbcreateSheet("Test Sheet 1",0);
//往工作表中添加数据
//1添加Label对象
Label label = new Label(0,0,"this is a label test");
wsaddCell(label);
//添加带有字型Formatting对象
WritableFont wf = new WritableFont(WritableFontTIMES,18,WritableFontBOLD,true);
WritableCellFormat wcf = new WritableCellFormat(wf);
Label labelcf = new Label(1,0,"this is a label test",wcf);
wsaddCell(labelcf);
//添加带有字体颜色的Formatting对象
WritableFont wfc = new WritableFont(WritableFontARIAL,10,WritableFontNO_BOLD,false,
UnderlineStyleNO_UNDERLINE,jxlformatColourRED);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);
wsaddCell(labelCF);
//2添加Number对象
Number labelN = new Number(0,1,31415926);
wsaddCell(labelN);
//添加带有formatting的Number对象
NumberFormat nf = new NumberFormat("###");
WritableCellFormat wcfN = new WritableCellFormat(nf);
Number labelNF = new jxlwriteNumber(1,1,31415926,wcfN);
wsaddCell(labelNF);
//3添加Boolean对象
Boolean labelB = new jxlwriteBoolean(0,2,false);
wsaddCell(labelB);
//4添加DateTime对象
jxlwriteDateTime labelDT = new jxlwriteDateTime(0,3,new javautilDate());
wsaddCell(labelDT);
//添加带有formatting的DateFormat对象
DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(1,3,new javautilDate(),wcfDF);
wsaddCell(labelDTF);
//添加对象,jxl只支持png格式
File image = new File("f:\\2png");
WritableImage wimage = new WritableImage(0,1,2,2,image);
wsaddImage(wimage);
//写入工作表
wwbwrite();
wwbclose();
}
catch(Exception e)
{
eprintStackTrace();
}
}
/
拷贝后,进行修改,其中file1为被copy对象,file2为修改后创建的对象
尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去,
以使单元格的内容以不同的形式表现
@param file1
@param file2
/
public static void modifyExcel(File file1,File file2)
{
try
{
Workbook rwb = WorkbookgetWorkbook(file1);
WritableWorkbook wwb = WorkbookcreateWorkbook(file2,rwb);//copy
WritableSheet ws = wwbgetSheet(0);
WritableCell wc = wsgetWritableCell(0,0);
//判断单元格的类型,做出相应的转换
if(wcgetType == CellTypeLABEL)
{
Label label = (Label)wc;
labelsetString("The value has been modified");
}
wwbwrite();
wwbclose();
rwbclose();
}
catch(Exception e)
{
eprintStackTrace();
}
}
以上就是关于如何用JAVA将数据库中的数据导入到excel表格全部的内容,包括:如何用JAVA将数据库中的数据导入到excel表格、在java程序中如果将数据库表导出为EX表、怎么用java把数据库里的数据写入到excel表中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)