10、Table的Excel导出功能
表格上的Excel导出功能还是比较实用的功能,企业应用一般都会用到,这里提供相关代码
02 | WritableCellFormat titleFormat = new WritableCellFormat( |
03 | new WritableFont(WritableFont.createFont( "黑体" ), 16 , |
04 | WritableFont.NO_BOLD)); |
05 | titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE); |
06 | titleFormat.setAlignment(Alignment.CENTRE); |
07 | titleFormat.setWrap( true ); |
09 | WritableCellFormat headerFormat = new WritableCellFormat(); |
10 | headerFormat.setBorder(Border.ALL, BorderLineStyle.THIN); |
11 | headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE); |
12 | headerFormat.setAlignment(Alignment.CENTRE); |
13 | headerFormat.setWrap( true ); |
15 | WritableCellFormat countFormat = new WritableCellFormat( |
16 | new NumberFormat( "0.000" )); |
17 | countFormat.setBorder(Border.ALL, BorderLineStyle.THIN); |
18 | countFormat.setVerticalAlignment(VerticalAlignment.CENTRE); |
19 | countFormat.setAlignment(Alignment.RIGHT); |
20 | countFormat.setWrap( true ); |
22 | WritableCellFormat moneyFormat = new WritableCellFormat( |
23 | new NumberFormat( "0.00" )); |
24 | moneyFormat.setBorder(Border.ALL, BorderLineStyle.THIN); |
25 | moneyFormat.setVerticalAlignment(VerticalAlignment.CENTRE); |
26 | moneyFormat.setAlignment(Alignment.RIGHT); |
27 | moneyFormat.setWrap( true ); |
29 | WritableCellFormat intFormat = new WritableCellFormat( |
30 | new NumberFormat( "0" )); |
31 | intFormat.setBorder(Border.ALL, BorderLineStyle.THIN); |
32 | intFormat.setVerticalAlignment(VerticalAlignment.CENTRE); |
33 | intFormat.setAlignment(Alignment.RIGHT); |
34 | intFormat.setWrap( true ); |
36 | WritableWorkbook book = Workbook.createWorkbook(exportFile); |
37 | WritableSheet sheet = book.createSheet( "综合统计报表" , 0 ); |
41 | sheet.mergeCells( 0 , titleRow, model.getColumnCount() - 1 , |
43 | Label titleLab = new Label( 0 , titleRow, tableTitleTxfd.getText(), titleFormat); |
44 | sheet.addCell(titleLab); |
47 | for ( int j = 0 ; j < model.getColumnCount(); j++) { |
48 | sheet.mergeCells(j, headerRow, j, headerRow + 1 ); |
49 | Label lab = new Label(j, headerRow, model.getColumnName( |
54 | for ( int row = 0 ; row < model.getRowCount(); row++) { |
55 | for ( int col = 0 ; col < model.getColumnCount(); col++) { |
56 | Object obj = model.getValueAt(row, col); |
57 | if (obj instanceof String) { |
58 | Label lab = new Label(col, dataRow + row, |
59 | (String) obj, headerFormat); |
61 | } else if (obj instanceof Integer) { |
62 | Number labelN = new Number(col, dataRow |
63 | + row, (Integer) obj, intFormat); |
64 | sheet.addCell(labelN); |
67 | new Label(col, dataRow + row, "" , |
75 | int footerRow = dataRow + model.getRowCount() + 1 ; |
77 | ( int ) ((( double ) (model.getColumnCount() - 2 ) / 3 ) |
79 | Label footerLab = new Label( 0 , footerRow, "部门负责人:" ); |
80 | sheet.addCell(footerLab); |
81 | footerLab = new Label(step, footerRow, "制表:" + creatorTxfd.getText()); |
82 | sheet.addCell(footerLab); |
83 | footerLab = new Label(model.getColumnCount() - 2 , footerRow, |
84 | NazcaFormater.getSimpleDateString( new Date())); |
85 | sheet.addCell(footerLab); |
87 | sheet.setColumnView( 0 , 16 ); |
88 | sheet.setRowView(titleRow, 600 ); |
93 | } catch (Throwable ex) { |
11、Table的打印功能
打印这块,之前已经提过,可以参考之前的文章 JTable Print
12、在Table上选择多行
1 | int rowcounts=table.getSelectedRows().length; |
3 | int [] rows=table.getSelectedRows(); |
4 | for ( int i= 0 ;i<rows.length;i++){ |
5 | String value=(String) tableModel.getValueAt(i, 1 ); |