package com.chai;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class excle {

// 工作簿
// 创建一个03版的
//注意路径最后要加反斜线
@Test
public void testwriter03(){

String lujing ="C:\\Users\\Administrator\\Desktop\\java项目\\excle\\";

FileOutputStream fileOutputStream=null;

Workbook workbook=new HSSFWorkbook();
// 工作表
Sheet sheet =workbook.createSheet("chai");
// 行
Row row1 =sheet.createRow(0);
// 列
Cell cell1 =row1.createCell(0);
cell1.setCellValue("我去fuck");
Cell cell2 =row1.createCell(1);
cell2.setCellValue("我去fuck2");
// 生成表

try {
fileOutputStream = new FileOutputStream(lujing+"chai03.xls");
} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
workbook.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
// 关闭流
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(fileOutputStream);
System.out.println("excle导出完毕");

}



@Test
public void testwriter07(){
// 03和07的区别就是对象不一样

String lujing ="C:\\Users\\Administrator\\Desktop\\java项目\\excle\\";

FileOutputStream fileOutputStream=null;

Workbook workbook=new XSSFWorkbook();
// 工作表
Sheet sheet =workbook.createSheet("chai");
// 行
Row row1 =sheet.createRow(0);
// 列
Cell cell1 =row1.createCell(0);
cell1.setCellValue("第一行第一列");
Cell cell2 =row1.createCell(1);
cell2.setCellValue("第一行第二列");

Row row2 =sheet.createRow(1);
// 列
Cell cell21 =row2.createCell(0);
cell21.setCellValue("第二行第一列");
Cell cell22 =row2.createCell(1);
cell22.setCellValue("第二行第二列");

// 生成表

try {
fileOutputStream = new FileOutputStream(lujing+"chai03.xlsx");
} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
workbook.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
// 关闭流
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(fileOutputStream);
System.out.println("excle导出完毕");




}





}
posted on 2022-04-14 11:15  Drirk-ROSE  阅读(35)  评论(0)    收藏  举报