- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将我的列 Mobile_Time 设置为时间/日期时间类型列。我已经尝试过
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
DAO d0 = new DAO();
Map<String, String> AllUsermap = new HashMap<String, String>();
AllUsermap = d0.colleagueMap(Integer.parseInt(request.getParameter("lid")), request.getParameter("ltype"));
String date = request.getParameter("date");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=All Employees Tracking Report [Date]:" + date + ".xls");
Workbook workbook = new HSSFWorkbook();
Font font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
CellStyle HeaderStyle = workbook.createCellStyle();
HeaderStyle.setFont(font);
for (String userK : AllUsermap.keySet()) {
String userinfo[] = userK.split("-");
String userinfoval[] = AllUsermap.get(userK).split("-");
if (userinfo.length == 2 && userinfoval.length == 3) {
Sheet sheet = workbook.createSheet(userinfoval[0]);
// header
Row row = sheet.createRow(0);
Cell serial = row.createCell(0);
serial.setCellValue("#");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(1);
serial.setCellValue("LOCATION");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(2);
serial.setCellValue("DISTANCE");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(3);
serial.setCellValue("BATTERY");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(4);
serial.setCellValue("SPEED");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(5);
serial.setCellValue("GPS_ACCURACY");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(6);
serial.setCellValue("STATUS");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(7);
serial.setCellType(1);
serial.setCellValue("MOBILE_TIME");
serial.setCellStyle(HeaderStyle);
serial = row.createCell(8);
serial.setCellValue("SPEED");
serial.setCellStyle(HeaderStyle);
// End Header
List<DailyReport> adr = new ArrayList<DailyReport>();
adr = d0.seletspecificdailyreport(Integer.parseInt(userinfo[0]), date, userinfo[1]);
if (adr.size() > 0) {
List<DailyReportDetails> adlrd = new ArrayList<DailyReportDetails>();
adlrd = d0.seletdailyreportdetails(adr.get(0).getIdDailyReport());
int i = 1;
for (DailyReportDetails drd : adlrd) {
row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(i);
cell = row.createCell(1);
cell.setCellValue(drd.getLocation());
cell = row.createCell(2);
cell.setCellValue(drd.getDistance());
cell = row.createCell(3);
cell.setCellValue(drd.getBattery());
cell = row.createCell(4);
cell.setCellValue(drd.getSpeed());
cell = row.createCell(5);
cell.setCellValue(drd.getAccuracy());
cell = row.createCell(6);
cell.setCellValue(drd.getOffline() == 1 ? "ONLINE" : "OFFLINE");
cell = row.createCell(7);
cell.setCellValue(drd.getMobiletime());
cell = row.createCell(8);
cell.setCellValue(userinfoval[2]);
i++;
}
}
}
}
workbook.write(response.getOutputStream()); // Write workbook to
// response.
workbook.close();
}
上层servlet正确写入xcel,但移动时间不是xcel中的时间或日期格式
我还尝试为 mobile_time 列序列添加行。
serial = row.createCell(7);
serial.setCellType(HSSFCell.LAST_COLUMN_NUMBER);
serial.setCellValue("MOBILE_TIME");
serial.setCellStyle(HeaderStyle);
并且还在生成行处尝试了以下行
cell = row.createCell(7);
try {
cell.setCellValue(new SimpleDateFormat("HH:mm:ss").parse(drd.getMobiletime()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但 mobile_time 列仍然是字符格式。正确的解决方案是什么?
最佳答案
你可以这样做:
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a date value in it. The first cell is not styled
// as a date.
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but other cells.
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);
//you can also set date as java.util.Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
关于java - 使用 HSSFSheet 将列格式类型设置为日期或时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44407555/
我有一个正在创建 Excel 工作簿的 servlet。 我让工作簿的第一张工作表按预期工作,并希望重用该代码,因为后续的一些工作表将具有相同的代码,但数据和格式不同。 我相信这使我能够将此代码放入其
我想将我的列 Mobile_Time 设置为时间/日期时间类型列。我已经尝试过 protected void doGet(HttpServletRequest request, HttpServlet
我正在尝试构建一个二维数组,该数组保存数据表中的值,然后获取这些值并将它们显示在网站申请表中。我不断得到 The method getCell(int) is undefined for the ty
我正在使用 POI 将一些数据导出到 .xslx,但有些列没有填充颜色,我使用循环,所以它应该可以工作,对于大多数列来说都是如此。那么出了什么问题? for (int i = 0; i < colum
将是 apache POI 中的一个非常棒的功能 HSSFBeanHelper.toHssfSheet(HSSFWorkbook workbook, List obj)这将从 Pojo 列表创建一个
我正在为将文档导出为 excel 创建一个脚本。 如何通过合并几个单元格来获得像“姓名:标记出生日期: 11-11-2014”这样的单元格值? 最佳答案 您需要做的是创建一个 RichTextStri
我正在尝试生成 Jasper Excel 报告。我收到类似 的异常 java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFShe
我是一名优秀的程序员,十分优秀!