如果觉得写得可以 或者太差 就 评论一下或者赞一下呗,多谢支持!!
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi excle 文件 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
2.
import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; 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 test { /** * @功能:手工构建一个简单格式的Excel */ public static void main(String[] args) { //excel的头 String[] head = {"第一列","第二列","第三列","第四列","第五列"}; //要存储的数据 ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>(); for(int i=0;i<10;i++){ ArrayList<Object> arrayList = new ArrayList(); arrayList.add(1); arrayList.add("你好呀"); arrayList.add(1.5); arrayList.add(new Date()); arrayList.add(1100); data.add(arrayList); } //要存储的路径 String path = "D:\\游戏\\sss.xls"; //创建第一个sheet HSSFWorkbook wb1 = createExcel(head,data, "用户表一"); //创建第二个sheet wb1 =addSheet(head,data, wb1,"用户表二"); try { FileOutputStream fout = new FileOutputStream(path); wb1.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } //创建第一个sheet public static HSSFWorkbook createExcel(String[] heads, ArrayList<ArrayList<Object>> data, String sheetName){ // 创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); return addSheet(heads,data ,wb,sheetName); } //创建第二个sheet public static HSSFWorkbook addSheet(String[] heads, ArrayList<ArrayList<Object>> data, HSSFWorkbook wb,String sheetName){ HSSFSheet sheet = wb.createSheet(sheetName); //在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); //创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell; for(int i=0;i<heads.length;i++){ cell = row.createCell((short)i); cell.setCellValue(heads[i]); cell.setCellStyle(style); } for (int i = 0; i < data.size(); i++) { row = sheet.createRow((int) i + 1); for(int j =0;j<(data.get(i)).size();j++){ if(data.get(i).get(j) instanceof String){ row.createCell((short)j).setCellValue(String.valueOf(data.get(i).get(j))); } if(data.get(i).get(j) instanceof Double){ row.createCell((short)j).setCellValue((Double) data.get(i).get(j)); } if(data.get(i).get(j) instanceof Integer){ row.createCell((short)j).setCellValue((Integer)data.get(i).get(j)); } if(data.get(i).get(j) instanceof Date){ row.createCell((short)j).setCellValue( new SimpleDateFormat("yyyy年mm月dd日").format(data.get(i).get(j))); } } } return wb; } }