Java Export Excel file

Source: Internet
Author: User

Recently in a file export function, found that most of the blog by reference to a variety of Util toolkit, in fact, or use Apache poi, in the project directly into the POI package can be. In the face of its principles, packaged with personal preferences.

1, first prepare some POI jar package

2, ready jar package can write code, then I will write the code simple, so that everyone can read. As for how much you want to use the code to achieve it, you can, as long as the principle of understanding, the other is not a thing, right.

First I'm going to build an entity class: Student.class

package testExport; /** *  * @author lf * */ public class Student {    private Integer id;    private String name;    private String sex;         public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }}还有一个导出类  ExportExcel.classpackage testExport; import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List; import javax.swing.JOptionPane; //下面是和数据导出有关的包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 ExportExcel {    public void Export(){        // 声明一个工作薄        HSSFWorkbook wb = new HSSFWorkbook();        //声明一个单子并命名        HSSFSheet sheet = wb.createSheet(学生表);        //给单子名称一个长度        sheet.setDefaultColumnWidth((short)15);        // 生成一个样式          HSSFCellStyle style = wb.createCellStyle();        //创建第一行(也可以称为表头)        HSSFRow row = sheet.createRow(0);        //样式字体居中        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);                         //添加一些数据,这里先写死,大家可以换成自己的集合数据               List<student> list = new ArrayList<student>();               list.add(new Student(111,张三,男));               list.add(new Student(111,李四,男));               list.add(new Student(111,王五,女));                //向单元格里填充数据               for (short i = 0; i < list.size(); i++) {                row = sheet.createRow(i + 1);                row.createCell(0).setCellValue(list.get(i).getId());                row.createCell(1).setCellValue(list.get(i).getName());                row.createCell(2).setCellValue(list.get(i).getSex());            }                        try {            //默认导出到E盘下            FileOutputStream out = new FileOutputStream(E://学生表.xls);            wb.write(out);            out.close();            JOptionPane.showMessageDialog(null, 导出成功!);        } catch (FileNotFoundException e) {            JOptionPane.showMessageDialog(null, 导出失败!);            e.printStackTrace();        } catch (IOException e) {            JOptionPane.showMessageDialog(null, 导出失败!);            e.printStackTrace();        }    }}Here's a look at:

Java Export Excel file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.