Java读取excel内容:
public class excel { public static void main(String[] args) throws BiffException, IOException { // TODO Auto-generated method stub Workbook book = Workbook.getWorkbook(new File( "C://Test.xls")); Sheet sheet = book.getSheet(0); List<String> list = new ArrayList<String>(); for (int k = 0; k < 5; k++) { for (int j = 0; j < 5; j++) { Cell cell = sheet.getCell(j, k);// 列,行 Cell keycell = sheet.getCell(j, 1); String Content = cell.getContents(); list.add(Content); } } for (String string : list) { System.out.println(string); } book.close(); } }
Java导出excel:
String filePath = "D:\\java\\myeclipse\\goodtaste_system" + "/upload/OrderRecord" + dateTime + ".xls"; filePath = request.getSession().getServletContext() .getRealPath(ConstantFiled.UPLOAD) + "/OrderRecord" + dateTime + ".xls"; OutputStream os = new FileOutputStream(filePath); book = Workbook.createWorkbook(os); WritableSheet sheet = book.createSheet("订单", 0); WritableFont wfont = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat titleFormat = new WritableCellFormat(wfont); String[] title = { "订单号", "桌号", "点餐时间", "结账时间", "消费人数", "总价" }; for (int i = 0; i < title.length; i++) { Label excelTitle = new Label(i, 0, title[i], titleFormat); sheet.addCell(excelTitle); } List<Order> list = new ArrayList<Order>(); list = this.getOrderService() .getAllOrdersByDateTime(dateTime + "%"); if (!list.isEmpty()) { for (int i = 0; i < list.size(); i++) { sheet.addCell(new Label(0, i + 1, list.get(i).getOrderId())); sheet.addCell(new Label(1, i + 1, list.get(i).getTableNum())); sheet.addCell(new Label(2, i + 1, sdf.format(list.get(i) .getOrderTime()))); sheet.addCell(new Label(3, i + 1, sdf.format(list.get(i) .getAccountTime()))); sheet.addCell(new Label(4, i + 1, String.valueOf(list .get(i).getConsmNums()))); sheet.addCell(new Label(5, i + 1, list.get(i) .getTotalPrice())); } } book.write();
Java下载文件:
String path = "D:\\java\\myeclipse\\goodtaste_system" + "/upload/OrderRecord" + dateTime + ".xls"; path = request.getSession().getServletContext() .getRealPath(ConstantFiled.UPLOAD) + "/OrderRecord" + dateTime + ".xls"; File file = new File(path); String filename = file.getName(); String ext = filename.substring(filename.lastIndexOf(".") + 1) .toUpperCase(); InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream( response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close();