public static void writeExcel(String fileDir,String sheetName, List> dataList){
createExcel(fileDir, sheetName, dataList);
//创建workbook
File file = new File(fileDir);
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook(new FileInputStream(file));
} catch(IOException e) {
e.printStackTrace();
}
//流
FileOutputStream out = null;
HSSFSheet sheet = workbook.getSheet(sheetName);
// 获取表格的总行数
int rowCount = sheet.getLastRowNum() + 1; // 需要加一
// 获取表头的列数
int columnCount = sheet.getRow(0).getLastCellNum();
//日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
HSSFCellStyle dateCellStyle=workbook.createCellStyle();
short df=workbook.createDataFormat().getFormat("yyyy-mm-dd");
dateCellStyle.setDataFormat(df);
try {
Row row = null;
// 获得表头行对象
HSSFRow titleRow = sheet.getRow(0);
Date temp = null;
if(titleRow!=null){
for (int i = 0, len = dataList.size(); i < len; i++) {
row = sheet.createRow(rowCount+i);//最新要添加的一行
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { // 遍历表头
String title = titleRow.getCell(columnIndex).toString().trim();
String data = dataList.get(i).get(title).toString();
Cell cell = row.createCell(columnIndex);
try{
temp = sdf.parse(data);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(temp);
cell.setCellStyle(dateCellStyle);
sheet.setColumnWidth(columnIndex, 10 * 256);
} catch(Exception e){
cell.setCellValue(data);
}
}
}
}
out = new FileOutputStream(fileDir);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java利用POI写入Excel表中关于时间格式的问题。已解决,供参考
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
悬赏问题
- ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
- ¥15 寻一个支付宝扫码远程授权登录的软件助手app
- ¥15 解riccati方程组
- ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
- ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
- ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
- ¥50 树莓派安卓APK系统签名
- ¥65 汇编语言除法溢出问题
- ¥15 Visual Studio问题
- ¥20 求一个html代码,有偿