java怎么导出excel表格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java怎么导出excel表格相关的知识,希望对你有一定的参考价值。

可以使用POI开源的api:
1.首先下载poi-3.6-20091214.jar,下载地址如下:
http://download.csdn.net/detail/evangel_z/3895051

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


参考技术A

添加spire.xls.jar为依赖,将数组导出为excel:

    import com.spire.xls.ExcelVersion;

    import com.spire.xls.Workbook;

    import com.spire.xls.Worksheet;


    public class InsertArray

        public static void main(String[] args)


            //创建Workbook对象

            Workbook wb = new Workbook();

            //获取第一张工作表

            Worksheet sheet = wb.getWorksheets().get(0);

            //定义一维数据

            String[] oneDimensionalArray = new String[]"苹果", "梨子", "葡萄", "香蕉";

            //将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入

            sheet.insertArray(oneDimensionalArray, 1, 1, true);

            //定义二维数组

            String[][] twoDimensionalArray = new String[][]

                    "姓名", "年龄", "性别", "学历",

                    "小张", "25", "男", "本科",

                    "小王", "24", "男", "本科",

                    "小李", "26", "女", "本科"

            ;

            //从指定单元格开始写入二维数组到工作表

            sheet.insertArray(twoDimensionalArray, 1, 3);

            //保存文档

            wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);

       

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; i<users.length;i++) // 相当于excel表格中的总行数
PropertyDescriptor[] descriptors=getAvailablePropertyDescriptors(users[i]);
rows[i]=s.createRow(i);
for (int j=0; descriptors!=null&&j<descriptors.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();


参考技术A 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 List<Student> 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();


参考技术B 平台使用Eclipse,支持包可选用Apache POI的Java API,Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)
建议查阅该文章:http://zc985552943.iteye.com/blog/1491546,并且附件有示例程序下载,有问题可联系……
参考技术C 使用POI插件,实现起来比较容易

以上是关于java怎么导出excel表格的主要内容,如果未能解决你的问题,请参考以下文章

java使用poi导出excel表格,可以导出到服务器,怎么才能让客户端进行选择导出的excel的路径呢

epplus 导出excel怎么设置excel表头

如何导出生成excel文件 java

用java将数据导出到wps表格中,怎么实现

如何让java利用POI导出excel表,并在Excel表中根据表格的数据生成柱形图。要求柱形图是动态的。

jxl导出excel出现乱码怎么解决