`
<冫茖>
  • 浏览: 35766 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Swing Tips(二)

阅读更多
10、Table的Excel导出功能

表格上的Excel导出功能还是比较实用的功能,企业应用一般都会用到,这里提供相关代码

01 try {
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); // 是否换行
08   
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); // 是否换行
14   
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); // 是否换行
21   
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); // 是否换行
28   
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); // 是否换行
35   
36    WritableWorkbook book = Workbook.createWorkbook(exportFile);
37    WritableSheet sheet = book.createSheet("综合统计报表", 0);
38    int titleRow = 0;
39    int headerRow = 3;
40    int dataRow = 5;
41    sheet.mergeCells(0, titleRow, model.getColumnCount() - 1,
42    titleRow);
43    Label titleLab = new Label(0, titleRow, tableTitleTxfd.getText(), titleFormat);
44    sheet.addCell(titleLab);
45   
46    //生成表头
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(
50       j), headerFormat);
51        sheet.addCell(lab);
52    }
53   
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);
60             sheet.addCell(lab);
61           } else if (obj instanceof Integer) {
62          Number labelN = new Number(col, dataRow
63          + row, (Integer) obj, intFormat);
64          sheet.addCell(labelN);
65          } else {
66          Label lab =
67          new Label(col, dataRow + row, "",
68          headerFormat);
69          sheet.addCell(lab);
70          }
71       }
72    }
73   
74    //生成表尾
75    int footerRow = dataRow + model.getRowCount() + 1;
76    int step =
77    (int) (((double) (model.getColumnCount() - 2) / 3)
78    + 1);
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);
86   
87    sheet.setColumnView(0, 16);
88    sheet.setRowView(titleRow, 600);
89   
90    book.write();
91    book.close();
92    //导出成功
93    } catch (Throwable ex) {
94    //导出报表失败
95    ex.printStackTrace();
96 }
11、Table的打印功能

打印这块,之前已经提过,可以参考之前的文章 JTable Print

12、在Table上选择多行
1 int rowcounts=table.getSelectedRows().length;
2 if(rowcounts>1)
3   int[] rows=table.getSelectedRows();
4     for(int i=0;i<rows.length;i++){
5     String value=(String) tableModel.getValueAt(i, 1);
6   }
7 }
分享到:
评论

相关推荐

    swing-tips-helper(swing-tips 例子助手)包含swing-tips的900多种swing各种实现例子

    1. 可以用来作为学习swing的辅助工具 ...3. 使用方式 java -jar swing-tips-helper.jar ,如果你需要源码及资源及更多详细介绍参考该地址 https://blog.csdn.net/x308561498/article/details/123211860

    java-swing-tips, 包含 Java Swing的示例.zip

    java-swing-tips, 包含 Java Swing的示例 java-swing-tips英语: 使用小源代码示例介绍 Java Swing的GUI 程序。 许可证https://github.com/aterai/java-swing-tips/blob/master/L

    java源码blob-java-swing-tips:JavaSwing示例

    java源代码块java-swing-tips :介绍一个带有源代码的小样本的Java Swing GUI程序。 :使用小源代码示例介绍Java Swing的GUI程序。 什么是摇摆 Swing是一组Java标准组件(库、 UI工具包),用于创建GUI (图形用户...

    A-Practical-Guide-to-LATEX-Tips.pdf

    The aim is not to teach LATEX programming, but to give a quick reference to all the tips and tricks that can be used if you are encountering a (difficult) problem, or simply facing a question which ...

    Delphi简单的FTP服务器

    简单的FTP服务器 ... FTP Server By: Bryan Cairns ...If youre a Dephi Pro, you can probably give me some tips as I am still new to Delphi programming. Bryan Cairns cairnsb@ameritech.net

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Swing and the Model-View-Controller Design Pattern 362 Introduction to Layout Management 368 Text Input 377 Choice Components 385 Menus 406 Sophisticated Layout Management 424 Dialog Boxes 452 ...

    Java: A Beginner's Guide 5th Edition

    Ask the Expert--Q&A sections filled with bonus information and helpful tips Try This--Hands-on exercises that show you how to apply your skills Self Tests--End-of-chapter questions that test your ...

    Filthy Rich Clients: Developing Animated and Graphical Effects for Desktop Java Applications (Part 1)

    Learn how to maximize the flexibility of these libraries and use them most effectively. * Performance: Follow in-depth discussions and tips throughout the book that will help you write high-...

    java.核心技术.第八版 卷1

    For detailed coverage of XML processing, networking, databases, internationalization, security, advanced AWT/Swing, and other advanced features, look for the forthcoming eighth edition of Core Java™,...

    Getting started with IntelliJ IDEA

    This book will navigate through the features of IntelliJ IDEA 12 and present the reader with concepts and tips that will give them a greater understanding of this IDE. Using a practical real-world ...

    Thinking in Java 4th Edition

    What’s Inside Preface 1 Java SE5 and SE6 .................. 2 Java SE6 ............................................The 4th edition...........................Changes ...........................................

    Visual C++ 编程资源大全(英文源码 控件)

    1,01.zip 3D Text 显示3D文字(6KB)&lt;END&gt;&lt;br&gt;2,02.zip A button within a button 按纽中的按纽(13KB)&lt;END&gt;&lt;br&gt;3,03.zip Flat Owner Drawn Buttons 浮动的自画按纽(13KB)&lt;END&gt;&lt;br&gt;4,04.zip Flat-...

    Java邮件开发Fundamentals of the JavaMail API

    1. Tutorial tips 2 2. Introducing the JavaMail API 3 3. Reviewing related protocols 4 4. Installing JavaMail 6 5. Reviewing the core classes 8 6. Using the JavaMail API 13 7. Searching ...

Global site tag (gtag.js) - Google Analytics