本文目录一览:

java怎么导出excel表格

通过这个例子,演示以下如何用java生成excel文件:

import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;

publicclass CreateCells

{

publicstaticvoid main(String[] args)

throws IOException

{

HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象

HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象

// Create a row and put some cells in it. Rows are 0 based.

HSSFRow row = sheet.createRow((short)0);//建立新行

// Create a cell and put a value in it.

HSSFCell cell = row.createCell((short)0);//建立新cell

cell.setCellValue(1);//设置cell的整数类型的值

// Or do it on one line.

row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值

row.createCell((short)2).setCellValue("test");//设置cell字符类型的值

row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值

HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式

cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式

HSSFCell dCell =row.createCell((short)4);

dCell.setCellValue(new Date());//设置cell为日期类型的值

dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式

HSSFCell csCell =row.createCell((short)5);

csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断

csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串

row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

}

}

如何用java将数据库中的表导出到excel

第一步:如何用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("你也好");

FileOutputStream fileOut = new FileOutputStream("d:/a/b.xls");

wb.write(fileOut);

fileOut.close();

}

第二步:导出指定数据库的所有表

分析:

1:某个数数据库有多少表,表名是什么?―――DataBaseMetadate.getMetadate().getTables(null,null,null,new String[]{Table}); - excel的文件名称。

2:对每一个表进行select * 操作。 - 每一个sheet的名称。

3:分析表结构,rs.getMetadate(); ResultSetMedated

4:多个列,列名是什么.- 字段名就是sheet的第一行信息。

5:获取每一行的数据 – 放到sheet第一行以后。

@Test

public void export() throws Exception{

//声明需要导出的数据库

String dbName = "focus";

//声明book

HSSFWorkbook book = new HSSFWorkbook();

//获取Connection,获取db的元数据

Connection con = DataSourceUtils.getConn();

//声明statemen

Statement st = con.createStatement();

//st.execute("use "+dbName);

DatabaseMetaData dmd = con.getMetaData();

//获取数据库有多少表

ResultSet rs = dmd.getTables(dbName,dbName,null,new String[]{"TABLE"});

//获取所有表名 - 就是一个sheet

ListString tables = new ArrayListString();

while(rs.next()){

String tableName = rs.getString("TABLE_NAME");

tables.add(tableName);

}

for(String tableName:tables){

HSSFSheet sheet = book.createSheet(tableName);

//声明sql

String sql = "select * from "+dbName+"."+tableName;

//查询数据

rs = st.executeQuery(sql);

//根据查询的结果,分析结果集的元数据

ResultSetMetaData rsmd = rs.getMetaData();

//获取这个查询有多少行

int cols = rsmd.getColumnCount();

//获取所有列名

//创建第一行

HSSFRow row = sheet.createRow(0);

for(int i=0;icols;i++){

String colName = rsmd.getColumnName(i+1);

//创建一个新的列

HSSFCell cell = row.createCell(i);

//写入列名

cell.setCellValue(colName);

}

//遍历数据

int index = 1;

while(rs.next()){

row = sheet.createRow(index++);

//声明列

for(int i=0;icols;i++){

String val = rs.getString(i+1);

//声明列

HSSFCell cel = row.createCell(i);

//放数据

cel.setCellValue(val);

}

}

}

con.close();

book.write(new FileOutputStream("d:/a/"+dbName+".xls"));

}

java怎么实现导出excel

偶将最近写了两个导出excel的方法,第一个是面向过程的思路,就是在指定的单元格写入指定的值,如下:

/**

*负责数据导入到EXCEL

*

* @param realPath

* EXCEL表格存放的绝对路径

* @param sheetname

*

* @param xLocation

* EXCEL表格的行索引,从1开始

* @param yLocation

* EXCEL表格的列索引,从1开始

* @param value

* 需要导入的数据

*

*/

public void modifyExcel(String realPath,String sheetname,int xLocaion,int yLocation,String value){

POIFSFileSystem fs=null;

HSSFWorkbook wb=null;

try {

File file=new File(realPath);

if(file.exists()){

fs = new POIFSFileSystem(new FileInputStream(realPath));

wb=new HSSFWorkbook(fs);

HSSFSheet s=wb.getSheetAt(0);

//函数处理时横纵坐标从索引0开始

HSSFRow row=s.getRow(xLocaion-1);

HSSFCell cell=null;

if(row!=null){

cell=row.getCell(yLocation-1);

if(cell==null){

cell=row.createCell(yLocation-1);

}

}else{

row=s.createRow(xLocaion-1);

cell=row.createCell(yLocation-1);

}

cell.setCellValue(value);

}else{

wb=new HSSFWorkbook();

HSSFSheet s=wb.createSheet();

wb.setSheetName(0, sheetname);

HSSFRow row=s.createRow(xLocaion-1);

HSSFCell cell=row.createCell(yLocation-1);

cell.setCellValue(value);

}

FileOutputStream fos=new FileOutputStream(realPath);

wb.write(fos);

fos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

第二种就是运用了对象,以对象为单位写入数据,即一个对象的所有属性在一行列出,有多少个对象就有多少行,此方法比较适用于个人信息导出之类的应用,至于导出属性的顺序问题在导出对象的实体类内部改动下即可:

/**

*负责数据导入到EXCEL

*

* @param realPath

* EXCEL表格存放的绝对路径

* @param sheetname

*

* @param users

* 需要导出到excel表的对象数组

*/

public void outputExcel(String realPath,String sheetname,UserModel[] users){

FileOutputStream fos;

try {

File file=new File(realPath);

fos = new FileOutputStream(file, true);

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet s=wb.createSheet();

wb.setSheetName(0, sheetname);

HSSFRow[] rows=new HSSFRow[users.length];

HSSFCell[][] cells=new HSSFCell[20][20];

for (int i=0; iusers.length;i++) { // 相当于excel表格中的总行数

PropertyDescriptor[] descriptors=getAvailablePropertyDescriptors(users[i]);

rows[i]=s.createRow(i);

for (int j=0; descriptors!=nulljdescriptors.length;j++) {

java.lang.reflect.Method readMethod = descriptors[j]

.getReadMethod();

cells[i][j]=rows[i].createCell(j);

Object value=readMethod.invoke(users[i], null);

cells[i][j].setCellValue(value.toString());

}

}

wb.write(fos);

fos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

Java列表的内容怎么导成Excel表格呢?

可以用jxl进行Execl的导出,jxl的机制是一个一个的单元格画出来的

我的导出是将table中的每行放到一个list中成为rowList,然后在把每行的rowList放到一个大的List中,这样一来,大的List就包含了所有的数据,然后在jxl中先遍历大的List,然后在循环中在遍历rowList就可以获取所有的数据.

for

(int

i

=

0;

i

20;

i++)

{

for

(int

j

=

0;

j

20;

j++)

{

label

=

new

Label(i,j,

"["+i+","+j+"]",

wcfFCCellTitle);

ws.addCell(label);

}

}

java怎么把数据库的内容导出到excel表里面

第一步:如何用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("你也好");

FileOutputStream fileOut = new FileOutputStream("d:/a/b.xls");

wb.write(fileOut);

fileOut.close();

}

第二步:导出指定数据库的所有表

分析:

1:某个数数据库有多少表,表名是什么?―――DataBaseMetadate.getMetadate().getTables(null,null,null,new String[]{Table}); - excel的文件名称。

2:对每一个表进行select * 操作。 - 每一个sheet的名称。

3:分析表结构,rs.getMetadate(); ResultSetMedated

4:多个列,列名是什么.- 字段名就是sheet的第一行信息。

5:获取每一行的数据 – 放到sheet第一行以后。

@Test

public void export() throws Exception{

//声明需要导出的数据库

String dbName = "focus";

//声明book

HSSFWorkbook book = new HSSFWorkbook();

//获取Connection,获取db的元数据

Connection con = DataSourceUtils.getConn();

//声明statemen

Statement st = con.createStatement();

//st.execute("use "+dbName);

DatabaseMetaData dmd = con.getMetaData();

//获取数据库有多少表

ResultSet rs = dmd.getTables(dbName,dbName,null,new String[]{"TABLE"});

//获取所有表名 - 就是一个sheet

ListString tables = new ArrayListString();

while(rs.next()){

String tableName = rs.getString("TABLE_NAME");

tables.add(tableName);

}

for(String tableName:tables){

HSSFSheet sheet = book.createSheet(tableName);

//声明sql

String sql = "select * from "+dbName+"."+tableName;

//查询数据

rs = st.executeQuery(sql);

//根据查询的结果,分析结果集的元数据

ResultSetMetaData rsmd = rs.getMetaData();

//获取这个查询有多少行

int cols = rsmd.getColumnCount();

//获取所有列名

//创建第一行

HSSFRow row = sheet.createRow(0);

for(int i=0;icols;i++){

String colName = rsmd.getColumnName(i+1);

//创建一个新的列

HSSFCell cell = row.createCell(i);

//写入列名

cell.setCellValue(colName);

}

//遍历数据

int index = 1;

while(rs.next()){

row = sheet.createRow(index++);

//声明列

for(int i=0;icols;i++){

String val = rs.getString(i+1);

//声明列

HSSFCell cel = row.createCell(i);

//放数据

cel.setCellValue(val);

}

}

}

con.close();

book.write(new FileOutputStream("d:/a/"+dbName+".xls"));

}