/** 导出JTable到Excel */ public void exportTable(JTable table, File file) throws IOException { try { OutputStream out = new FileOutputStream(file); TableModel model = table.getModel(); WritableWorkbook wwb = Workbook.createWorkbook(out); WritableSheet ws = wwb.createSheet("关联规则", 0); for (int i = 0; i < model.getColumnCount() - 1; i++) { jxl.write.Label labelN = new jxl.write.Label(i, 0, model.getColumnName(i + 1)); try { ws.addCell(labelN); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException we) { we.printStackTrace(); } } int row = 1; for (int j = 1; j <= model.getRowCount(); ++j) { if (model.getValueAt(j - 1, 0).equals(true)) { for (int i = 0; i < model.getColumnCount() - 1; ++i) { jxl.write.Label labelN = new jxl.write.Label(i, row, model.getValueAt(j - 1, i + 1).toString()); try { ws.addCell(labelN); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException we) { we.printStackTrace(); } } row++; } } wwb.write(); try { wwb.close(); out.close(); } catch (WriteException e) { e.printStackTrace(); } } catch (Exception e) { } }
public static void write2Cell(WritableSheet ws, int c, int r, String item) { // System.out.println(index+r); // 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行 Label labelC = new Label(c, r, item); try { // 将生成的单元格添加到工作表中 ws.addCell(labelC); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } }
@Test public void testExportToCollection() throws BiffException, IOException { ExcelUtil excelUtil = new ExcelUtil(); excelUtil.openUploadExcel("collectionTemplate.xls", 0); ModelCollection collection = excelUtil.getCollection(); System.out.println(collection); excelUtil.closeExcel(); excelUtil.openDownloadNewExcel("collectionTemplate.xls", 0); try { excelUtil.exoprtToExcel(collection); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public void removeAll(List<? extends Persistable> objList) { try { xclAdaptee.write(file, new ArrayList<ArrayList<String>>()); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * 生成一个Excel文件 * * @param fileName 要生成的Excel文件名 */ public static void writeExcel(String fileName) { WritableWorkbook wwb = null; try { // 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象 wwb = Workbook.createWorkbook(new File(fileName)); } catch (IOException e) { e.printStackTrace(); } if (wwb != null) { // 创建一个可写入的工作表 // Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置 WritableSheet ws = wwb.createSheet("sheet1", 0); // 下面开始添加单元格 for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { // 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行 Label labelC = new Label(j, i, "这是第" + (i + 1) + "行,第" + (j + 1) + "列"); try { // 将生成的单元格添加到工作表中 ws.addCell(labelC); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } try { // 从内存中写入文件中 wwb.write(); // 关闭资源,释放内存 wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
public void addData(DataChannel c) { try { int col = 1; addLabel(lineOffset, 0, c.toString() + " Time (ms)"); addLabel(lineOffset + 1, 0, c.toString() + " Value"); for (Object o : c.getSeries().getItems()) { XYDataItem i = (XYDataItem) o; addNumber(lineOffset, col, i.getXValue()); addNumber(lineOffset + 1, col, i.getYValue()); col++; } } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } lineOffset += 2; }
/** * The doPost method of the servlet. <br> * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gb2312"); request.setCharacterEncoding("gb2312"); HttpSession session = request.getSession(); ArrayList adminlogin = (ArrayList) session.getAttribute("adminlogin"); shuchu shu = new shuchu(); try { shu.shuchu(adminlogin.get(1).toString()); request.setAttribute("message", "'成绩已打印(位置:D:/输出成绩.xls)'"); request .getRequestDispatcher("/admin/myscore.jsp?verifyType=" + request.getParameter("name")) .forward(request, response); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public void addAll(List<? extends Persistable> objList) { ArrayList<ArrayList<String>> serializedList = new ArrayList<>(); for (Persistable p : objList) { ArrayList<String> lineLs = p.serializeToStringArray(); serializedList.add(lineLs); } try { xclAdaptee.write(file, serializedList); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public void run(IAction action) { File file; try { view = Activator.getDefault() .getWorkbench() .getActiveWorkbenchWindow() .getActivePage() .findView(AddressTreeView.ID); file = File.createTempFile( "output_" + new Date().getTime(), ".xls"); // $NON-NLS-1$ //$NON-NLS-2$ WritableWorkbook workbook = Workbook.createWorkbook(file); WritableSheet sheet = workbook.createSheet("Export", 0); // $NON-NLS-1$ sheet.addCell(new Label(0, 0, Messages.CompanyEditor_Label_Denomination)); sheet.addCell(new Label(1, 0, Messages.ScrolledCRMFormEditor_Label_Title)); sheet.addCell(new Label(2, 0, Messages.ScrolledCRMFormEditor_Label_Firstname)); sheet.addCell(new Label(3, 0, Messages.ScrolledCRMFormEditor_Label_Lastname)); sheet.addCell(new Label(4, 0, Messages.CompanyEditor_Label_Street)); sheet.addCell(new Label(5, 0, Messages.CompanyEditor_Label_Zip)); sheet.addCell(new Label(6, 0, Messages.CompanyEditor_Label_City)); sheet.addCell(new Label(7, 0, Messages.CompanyEditor_Label_State)); sheet.addCell(new Label(8, 0, Messages.CompanyEditor_Label_Country)); sheet.addCell(new Label(9, 0, Messages.CompanyEditor_Label_Phone)); sheet.addCell(new Label(10, 0, Messages.CompanyEditor_Label_Mobile)); sheet.addCell(new Label(11, 0, Messages.CompanyEditor_Label_Fax)); sheet.addCell(new Label(12, 0, Messages.CompanyEditor_Label_Email)); sheet.addCell(new Label(13, 0, Messages.CompanyEditor_Label_Website)); sheet.addCell(new Label(14, 0, Messages.CompanyEditor_Label_Note)); int i = 1; Addressbook book = (Addressbook) ConnectionFactory.getConnection("AddressbookBean"); // $NON-NLS-1$ for (Company company : ((AddressTreeView) view).getCompanies()) { company = (Company) book.getAddress(company); sheet.addCell(new Label(0, i, company.getDenomination())); sheet.addCell(new Label(1, i, company.getContact_title())); sheet.addCell(new Label(2, i, company.getContact_firstname())); sheet.addCell(new Label(3, i, company.getContact_lastname())); sheet.addCell(new Label(4, i, company.getStreet())); sheet.addCell(new Label(5, i, company.getZip())); sheet.addCell(new Label(6, i, company.getCity())); sheet.addCell(new Label(7, i, company.getState())); sheet.addCell(new Label(8, i, company.getCountry())); sheet.addCell(new Label(9, i, company.getPhone())); sheet.addCell(new Label(10, i, company.getMobilephone())); sheet.addCell(new Label(11, i, company.getFax())); sheet.addCell(new Label(12, i, company.getEmail())); sheet.addCell(new Label(13, i, company.getWebsite())); sheet.addCell(new Label(14, i, company.getNote())); i++; } workbook.write(); workbook.close(); Program.launch(file.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Throwable t) { // TODO Auto-generated catch block t.printStackTrace(); } }