Java import, export Excel
I. INTRODUCTION |
The current B / S model has become the mainstream application development, and in the business office systems, often requires a customer like this: Do you want us to open the report directly in Excel (telecommunications system, the banking system). Or: We have become accustomed to print with Excel. So our actual development, often need to implement import, export, use of Excel.
At present, more commonly used to achieve Java import, export Excel, there are two technologies Java and Jakarta POI Excel
Here I will explain how to use each of these two technology import, export Excel
Second, the use Jakarta POI import, export Excel |
Jakarta POI is a Java API for accessing Microsoft format documents. Jakarta POI have many components, including an operation HSSF Excel format file and for operating HWPF Word, the various components are present only for the operation of Excel HSSF relatively mature. The official home page http://poi.apache.org/index.html , API documentation http://poi.apache.org/apidocs/index.html
2.1 environment configuration
2.1.1 Download jar
Official Download: http://poi.apache.org/download.html here to download the latest version and documentation, the latest version is 3.7, where the use of more stable version 3.6.
2.1.2 join the jar package
The poi-3.6-20091214.jar and Lib directory under the root directory of the General Packet three commons-logging-1.1.jar junit-3.8.1.jar log4j-1.2.13.jar Lib copied to the project
2.2 Jakarta POI API HSSF 组件
HSSF (Excel for the assembly operation) to the user object in rg.apache.poi.hssf.usermodel package, comprising a main portion Excel objects, styles and formats, as well as auxiliary operation. There are several objects:
Common components:
HSSFWorkbook excel document object
HSSFSheet excel form
HSSFRow excel row
The lattice cell HSSFCell excel
HSSFFont excel font
HSSFDataFormat date format
HSSFHeader sheet头
HSSFFooter sheet tail (only when printing to see the results)
style:
HSSFCellStyle cell style
Auxiliary operations include:
HSSFDateUtil date
HSSFPrintSetup Print
HSSFErrorConstants error information table
2.3 Basic Procedure
First, understand how the organizational form of an Excel file, an Excel file corresponds to a workbook (HSSFWorkbook), a workbook can have multiple sheet (HSSFSheet) composed of a sheet is composed of multiple row (HSSFRow) composed of a row is a plurality of cell (HSSFCell) composition.
Basic steps:
1, with HSSFWorkbook open or create a "Excel file object."
2, or created with the object returns HSSFWorkbook Sheet object
3, the object returns Sheet object rows, row objects obtained by the object Cell
4, to read and write the object Cell.
Let's look at an example of dynamically generated Excel file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
HSSF read the file or use these same objects, only the corresponding createXXX way into a getXXX method can be. Once you understand the principles of which can be seen, whether it is read or written, or a specific format also can be easily achieved, so-called know these to know why.
2.4 Export Excel application examples
In 2.3 we have a few lines of code actually has been achieved is a simple example of export Excel, let's look at how to achieve export Excel spreadsheet as shown in FIG?
Code is as follows :( actual development business layer component to be encapsulated, and then calls the control layer. Here written directly execute a method of controlling layer assembly, such as a Servlet doGet / doPost method of the Struts framework)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
This code is underlined output B / S mode is employed, instead of outputting the specified disk to the local directory. This code indicates the response by details.xls Excel file entity (response) output to the requesting client browser, the client directly open or save.
2.5 style settings
Export Excel files in practical applications often need to read and print, which needs to be output to Excel document layout and style setting, significant operations are merged cells, set the cell style, set the font styles.
2.5.1 Merge cells
The use HSSFSheet addMergedRegion () method
1 |
|
CellRangeAddress parameter indicates the consolidated region is constructed as follows:
1 |
|
Configuration parameters in turn represents the starting line up to the line, start column, as of columns. Referring to section 2.4 Sample Code
2.5.2 High cell line sets, column width
1 2 3 4 5 6 7 |
|
2.5.2 cell styles
1. Create HSSFCellStyle
1 |
|
2, set the style
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
3, the style applied to the cell
1 2 3 4 5 |
|
2.5.2 set the font style
1. Create HSSFFont objects (call createFont method of HSSFWorkbook)
1 2 3 4 5 |
|
2, set the font variety of styles
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
3, the font style is set to the cell
1 2 3 4 5 6 7 8 9 |
|
2.6 导入Excel应用实例
实现将已存在的Excel文件中的数据导入到系统中的基本步骤同导出十分的类似,关键在于要了解要导入Excel文件的结构,比如数据有哪些列、读取数据起始位置(有效数据从第几行几列开始)等。在实际项目中由于这些数据(Excel文件)往往来自于日常办公人员或其他系统平台产生的业务数据,因此这些Excel文件的数据格式要有统一的要求,并提供访问接口(指访问途径),这样在所需数据的系统中就可通过提供这个访问接口调用方法,从而获得数据。解决方案采用Web Service是不错的选择。这里,我们就以导入2..4所产生的excel表为例,重点掌握如何编写导入Excel代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
三、使用java Excel操作Excel文件 |
Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。jxl 由于其小巧 易用的特点, 逐渐已经取代了 POI-excel的地位, 成为了越来越多的java开发人员生成excel文件的首选。Java Excel的特征:
● 支持Excel 95-2000的所有版本
● 生成Excel 2000标准格式
● 支持字体、数字、日期格式化操作
● 支持对单元格加阴影和加色彩;● 修改存在的工作表;
● 支持图像和图表● 日志记录可以定制
● 更小更快更省内存
应该说以上功能已经能够大致满足我们的需要。最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。在线帮助文档http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/index.html
在这里我们将通过一些实例,学习掌握读取、新建、更新,其中也包括常见格式的设置:字体、颜色、背景、合并单元格等操作,有这些其实已经基本足够应付大部分问题了。
3.1环境配置
3.1.1下载
下载地址 http://www.andykhan.com/jexcelapi/
3.1.2 加入jar包
将jxl.jar拷贝到项目的Lib下
3.2 使用Java Excel Api 导出 Excel文件
下面我们在看如何使用Java Excel实现导出Excel表格?
代码如下:(实际开发中应封装到业务层组件中,然后在控制层中调用。这里直接写在控制层组件,如Servlet的doGet/doPost方法或Struts框架的execute方法中)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
3.3高级操作
3.3.1数据格式化
在Excel中不涉及复杂的数据类型,能够比较好的处理字串、数字和日期已经能够满足一般的应用即可。
数据的格式化涉及到的是字体、粗细、字号等元素,这些功能主要由 WritableFont和WritableCellFormat类来负责。例如:
① WritableFont font=new WritableFont(WritableFont.createFont("宋体"),12,WritableFont.NO_BOLD );
② WritableCellFormat format1=new WritableCellFormat(font);
③ Label label=new Label(0,0,”data 4 test”,format1);
其中
I.指定了字串格式:字体为宋体,字号16,加粗显示。WritableFont有非常丰富的构造子,供不同情况下使用,jExcelAPI的java-doc中有详细列表,这里不再列出。
II. 处代码使用了WritableCellFormat类,这个类非常重要,通过它可以指定单元格的各种属性,如上例代码所示。
III. 处使用了Label类的构造子,指定了显示的位置,文本内容,字串被赋予的格式。
与Label类似的Number、DateTime,区别Label表示文本数据;Number表示数值数据,可使NumberFormat格式化数据;用DateTime表示日期型数据,可应用DateFormat格式化数据。
3.3.2单元格操作
Excel中很重要的一部分是对单元格的操作,比如行高、列宽、单元格合并等,所幸jExcelAPI提供了这些支持。这些操作相对比较简单,下面只介绍一下相关的API。
1、 合并单元格
1 2 3 4 5 6 7 8 9 10 11 |
|
2、 行高和列宽
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
3.4 从Excel文件读取数据表
我们就以导入3.2所产生的excel表为例,掌握如何编写导入Excel代码(该代码封装在业务层方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
3.4 更新已存在的Excel文件
将3.2所产生的excel表(temp.xls)的第一条记录(excel文件的指第三行)的班级名称改为As179,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
对于更新已存在的Excel文件实际上就是获取已有工作薄对象(但是只读的),然后将获取的只读的工作薄对象转化为可写入的Excel工作薄对象(WritableWorkbook ),其他部分就是通过可写入WritableSheet 对象和可写入WritableCell 对象进行编辑。
来自:http://blog.csdn.net/jerehedu/article/details/45195359