怎么用java把数据库里的数据写入到excel表中

怎么用java把数据库里的数据写入到excel表中,第1张

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文件中的数据导入SQLServer

打开需要导入以及被导入的工作薄;

在需要移动的工作表标签上单击鼠标右键,选择“移动或复制”;

在d出的对话框中,选择导入目标工作薄,选择“移至最后”或插入某一个工作表之前,如果是复制就勾选“建立副本”,不勾选的话就类似于剪切过去,然后确定;

这个工作表就完整的被移动到了目标工作薄中了

参考下面方法:

import javaioFileInputStream;­

import javaioFileNotFoundException;­

import javaioIOException;­

import orgapachecommonsloggingLog;­

import orgapachecommonsloggingLogFactory;­

import orgapachepoihssfusermodelHSSFCell;­

import orgapachepoihssfusermodelHSSFRow;­

import orgapachepoihssfusermodelHSSFSheet;­

import orgapachepoihssfusermodelHSSFWorkbook;­

public class TestExcel {­

//记录类的输出信息­

static Log log = LogFactorygetLog(TestExcelclass); ­

//获取Excel文档的路径­

public static String filePath = "D://excelxls";­

public static void main(String[] args) {­

try {­

// 创建对Excel工作簿文件的引用­

HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));­

// 在Excel文档中,第一张工作表的缺省索引是0

// 其语句为:HSSFSheet sheet = workbookgetSheetAt(0);­

HSSFSheet sheet = wookbookgetSheet("Sheet1");­

//获取到Excel文件中的所有行数­

int rows = sheetgetPhysicalNumberOfRows();

//遍历行­

for (int i = 0; i < rows; i++) {­

// 读取左上端单元格­

HSSFRow row = sheetgetRow(i);­

// 行不为空­

if (row != null) {­

//获取到Excel文件中的所有的列­

int cells = rowgetPhysicalNumberOfCells();­

String value = ""; ­

//遍历列­

for (int j = 0; j < cells; j++) {­

//获取到列的值­

HSSFCell cell = rowgetCell(j);­

if (cell != null) {­

switch (cellgetCellType()) {­

case HSSFCellCELL_TYPE_FORMULA:­

break;­

case HSSFCellCELL_TYPE_NUMERIC:­

value += cellgetNumericCellValue() + ","; ­

break; ­

case HSSFCellCELL_TYPE_STRING:­

value += cellgetStringCellValue() + ",";­

break;­

default:­

value += "0";­

break;­

}

// 将数据插入到mysql数据库中­

String[] val = valuesplit(",");­

TestEntity entity = new TestEntity();­

entitysetNum1(val[0]);­

entitysetNum2(val[1]);­

entitysetNum3(val[2]);­

entitysetNum4(val[3]);­

entitysetNum5(val[4]);­

entitysetNum6(val[5]);­

TestMethod method = new TestMethod();­

methodAdd(entity);­

} catch (FileNotFoundException e) {­

eprintStackTrace();­

} catch (IOException e) {­

eprintStackTrace();­

以上就是关于怎么用java把数据库里的数据写入到excel表中全部的内容,包括:怎么用java把数据库里的数据写入到excel表中、如何利用Java将Excel文件中的数据导入SQLServer、用java怎么将excel表格数据导入到mysql数据库中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10186625.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存