java_I/O操作_CreateEXcelFile
Posted -chang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java_I/O操作_CreateEXcelFile相关的知识,希望对你有一定的参考价值。
- //主函数
- public class MainDemo {
- public static void main(String[] args) throws Exception {
- ExcelDocument excelDoc = new ExcelDocument();
- excelDoc.createExcelFile07();
- }
- }
- //封装Students类
- import java.util.Date;
- import javax.xml.crypto.Data;
- public class Students {
- private String code;
- private String name;
- private Integer age;
- private Date birth;
- public Students(String code,String name,Integer age,Date birth) {
- this.code = code;
- this.name = name;
- this.age = age;
- this.birth = birth;
- }
- public String getCode() {
- return code;
- }
- protected void setCode(String code) {
- this.code = code;
- }
- public String getName() {
- return name;
- }
- protected void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- protected void setAge(Integer age) {
- this.age = age;
- }
- public Date getBirth() {
- return birth;
- }
- protected void setBirth(Date birth) {
- this.birth = birth;
- }
- }
- //创建Students类实例并输出到Excel中
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- import javax.jws.soap.SOAPBinding.Style;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFCellStyle;
- import org.apache.poi.xssf.usermodel.XSSFRow;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- public class ExcelDocument {
- //新建1997版Excel文档
- public static Workbook createExcelFile97() {
- Workbook wb = new HSSFWorkbook();
- return wb;
- }
- //新建2007版Excel文档
- public static void createExcelFile07() throws Exception {
- XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet sheet = wb.createSheet("学生表");//第二步:在workbook中添加一张sheet表
- XSSFRow row = sheet.createRow((int)0); //第三步:在sheet表中添加一行作为表头
- XSSFCellStyle style = (XSSFCellStyle)createStyle(wb);//第四步:设置样式
- //第五步:创建cell表头
- XSSFCell 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 = getStudents();
- for (int i = 0; i < list.size(); i++) {
- row = sheet.createRow((int)i+1);
- Students stu = (Students)list.get(i);
- row.createCell((int)0).setCellValue(stu.getCode());
- row.createCell((int)1).setCellValue(stu.getName());
- row.createCell((int)2).setCellValue(stu.getAge());
- row.createCell((int)3).setCellValue(stu.getBirth());
- }
- //第七步:将文件写入指定的位置
- FileOutputStream outStream = getFileOutputStream();
- wb.write(outStream);
- outStream.close();
- }
- //将文件写入指定的位置
- private static FileOutputStream getFileOutputStream() throws FileNotFoundException {
- String path = "E:" + File.separatorChar + "Members.xls";
- File file = new File(path);
- if (!file.getParentFile().exists()) { //如果父级路径不存在就创建父级目录
- file.getParentFile().mkdirs();
- if (!file.exists()) { //如果文件不存在就创建文件
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- FileOutputStream outputStream = new FileOutputStream(file);
- return outputStream;
- }
- //创建cell的样式
- private static CellStyle createStyle(Workbook wb) {
- CellStyle style= wb.createCellStyle();
- style.setAlignment(CellStyle.ALIGN_CENTER);//格式居中
- return style;
- }
- //创建数组
- private static List<Students> getStudents() throws Exception {
- List list = new ArrayList();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
- Students student1 = new Students("1", "张三", (Integer)24, dateFormat.parse("1993-08-28"));
- Students student2 = new Students("2", "李思", (Integer)23, dateFormat.parse("1994-03-07"));
- list.add(student1);
- list.add(student2);
- return list;
- }
- }
POI.jar链接:https://pan.baidu.com/s/1KGcwHJHfWUwLQifOJS5XQg 密码:xvd2
以上是关于java_I/O操作_CreateEXcelFile的主要内容,如果未能解决你的问题,请参考以下文章
java_I/O_合并流(SequenceInputStream)