import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
public class POITest {
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet1 = wb.createSheet("new sheet");
Row row = sheet1.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(1);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("C:/Users/KunChi/Downloads/hello.xls")){
wb.write(outputStream);
}
}
}
项目使用gradle 导入poi jar
ependencies {
implementation('org.apache.poi:poi:3.17')
}