本文目录一览:

java怎么导出excel表格

可以使用POI开源的api:

1.首先下载poi-3.6-20091214.jar,下载地址如下:

2.Student.java

import java.util.Date;

public class Student

{

private int id;

private String name;

private int age;

private Date birth;

public Student()

{

}

public Student(int id, String name, int age, Date birth)

{

this.id = id;

this.name = name;

this.age = age;

this.birth = birth;

}

public int getId()

{

return id;

}

public void setId(int id)

{

this.id = id;

}

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

public Date getBirth()

{

return birth;

}

public void setBirth(Date birth)

{

this.birth = birth;

}

}

3.CreateSimpleExcelToDisk.java

import java.io.FileOutputStream;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.List;

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

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

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

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

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

public class CreateSimpleExcelToDisk

{

/**

* @功能:手工构建一个简单格式的Excel

*/

private static ListStudent getStudent() throws Exception

{

List list = new ArrayList();

SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));

Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));

Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));

list.add(user1);

list.add(user2);

list.add(user3);

return list;

}

public static void main(String[] args) throws Exception

{

// 第一步,创建一个webbook,对应一个Excel文件

HSSFWorkbook wb = new HSSFWorkbook();

// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

HSSFSheet sheet = wb.createSheet("学生表一");

// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

HSSFRow row = sheet.createRow((int) 0);

// 第四步,创建单元格,并设置值表头 设置表头居中

HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

HSSFCell cell = row.createCell((short) 0);

cell.setCellValue("学号");

cell.setCellStyle(style);

cell = row.createCell((short) 1);

cell.setCellValue("姓名");

cell.setCellStyle(style);

cell = row.createCell((short) 2);

cell.setCellValue("年龄");

cell.setCellStyle(style);

cell = row.createCell((short) 3);

cell.setCellValue("生日");

cell.setCellStyle(style);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

List list = CreateSimpleExcelToDisk.getStudent();

for (int i = 0; i list.size(); i++)

{

row = sheet.createRow((int) i + 1);

Student stu = (Student) list.get(i);

// 第四步,创建单元格,并设置值

row.createCell((short) 0).setCellValue((double) stu.getId());

row.createCell((short) 1).setCellValue(stu.getName());

row.createCell((short) 2).setCellValue((double) stu.getAge());

cell = row.createCell((short) 3);

cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu

.getBirth()));

}

// 第六步,将文件存到指定位置

try

{

FileOutputStream fout = new FileOutputStream("E:/students.xls");

wb.write(fout);

fout.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

java怎样输出excel文件

//java生成简单的Excel文件

package beans.excel;

import java.io.IOException;

import java.io.OutputStream;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

 

public class SimpleExcelWrite {

    public void createExcel(OutputStream os) throws WriteException,IOException{

        //创建工作薄

        WritableWorkbook workbook = Workbook.createWorkbook(os);

        //创建新的一页

        WritableSheet sheet = workbook.createSheet("First Sheet",0);

        //创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容

        Label xuexiao = new Label(0,0,"学校");

        sheet.addCell(xuexiao);

        Label zhuanye = new Label(1,0,"专业");

        sheet.addCell(zhuanye);

        Label jingzhengli = new Label(2,0,"专业竞争力");

        sheet.addCell(jingzhengli);

        

        Label qinghua = new Label(0,1,"清华大学");

        sheet.addCell(qinghua);

        Label jisuanji = new Label(1,1,"计算机专业");

        sheet.addCell(jisuanji);

        Label gao = new Label(2,1,"高");

        sheet.addCell(gao);

        

        Label beida = new Label(0,2,"北京大学");

        sheet.addCell(beida);

        Label falv = new Label(1,2,"法律专业");

        sheet.addCell(falv);

        Label zhong = new Label(2,2,"中");

        sheet.addCell(zhong);

        

        Label ligong = new Label(0,3,"北京理工大学");

        sheet.addCell(ligong);

        Label hangkong = new Label(1,3,"航空专业");

        sheet.addCell(hangkong);

        Label di = new Label(2,3,"低");

        sheet.addCell(di);

        

        //把创建的内容写入到输出流中,并关闭输出流

        workbook.write();

        workbook.close();

        os.close();

    }

    

}

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();

}

}