LinkinPark
当你的才华撑不起你野心时,那么请潜下心继续学习,心无旁骛,愿多年以后你们我都能成为自己想象的模样。

文章转载自:易百教程 [http:/www.yiibai.com]

本文地址:http://www.yiibai.com/apache_poi/apache_poi_java_excel.html

POI教程

很多时候,一个软件应用程序需要生成Microsoft Excel文件格式的报告。有时,一个应用程序甚至希望将Excel文件作为输入数据。例如,一个公司开发的应用程序将财务部门需要所有输出生成自己的Excel。

任何Java程序员愿意将MS Office文件的输出,可以使用预定义和只读API来做到。

什么是Apache POI?

Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件。这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Office文件的开源库。它包含类和方法对用户输入数据或文件到MS Office文档进行解码。

Apache POI组件

Apache POI包含类和方法,来将MS Office所有OLE 2文档复合。此API组件的列表如下。

  • POIFS (较差混淆技术实现文件系统) : 此组件是所有其他POI元件的基本因素。它被用来明确地读取不同的文件。

  • HSSF (可怕的电子表格格式) : 它被用来读取和写入MS-Excel文件的xls格式。

  • XSSF (XML格式) : 它是用于MS-Excel中XLSX文件格式。

  • HPSF (可怕的属性设置格式) : 它用来提取MS-Office文件属性设置。

  • HWPF (可怕的字处理器格式) : 它是用来读取和写入MS-Word的文档扩展名的文件。

  • XWPF (XML字处理器格式) : 它是用来读取和写入MS-Word的docx扩展名的文件。

  • HSLF (可怕的幻灯片版式格式) : 它是用于读取,创建和编辑PowerPoint演示文稿。

  • HDGF (可怕的图表格式) : 它包含类和方法为MS-Visio的二进制文件。

  • HPBF (可怕的出版商格式) : 它被用来读取和写入MS-Publisher文件。

本教程将指导使用Java Excel文件完成工作过程。因此,本教程仅限于HSSF和XSSF组件。

注:旧版本的POI支持二进制文件格式,如DOC,XLS,PPT等从版本3.5起,POI支持微软Office的OOXML文件格式,如DOCX,XLSX,PPTX等。

如Apache POI,还有由不同的供应商为Excel文件的生成提供的其他库。这些措施包括Aspose面向Java的Aspose,JXL 通过共享库由JExcel团队开发。


本章将介绍一些Java ExcelAPI和它们的特征。有许多厂商谁提供Java Excel相关的API;其中一些将在这一章中讨论。

Java Aspose Cells

Java Aspose Cells 是一种纯粹的Java授权的Excel API,开发和供应商Aspose发布。这个API的最新版本是8.1.2,发布于2014年7月,是一个丰富而厚重的API(普通Java类和AWT类的组合)设计,可以读、写和操纵电子表格Excel的组件。此API常见用途如下:

  • Excel报表,建立动态Excel报表
  • 高保真的Excel渲染和打印
  • 从Excel电子表格中导入和导出数据
  • 生成,编辑,转换和电子表格

JXL

JXL是一款专为Selenium第三方框架,支持基于Web浏览器(数据是Web浏览器自动更新)数据驱动的自动化。然而,它也被用来作为JExcel API的一个共同的支持库,因为它的基本功能是可创建,读取和写入电子表格。基本特征如下:

  • 生成Excel文件
  • 从工作簿和电子表格导入数据
  • 获得行和列的总数

注意:JXL只支持xls档案格式,并且不能处理大数据量。

JExcel

JExcel是由Team Dev开发提供纯行货API。利用这一点程序员可以很容易地读取,写入,显示和修改Excel工作簿中的两种格式:.xls和.XLSX。这个API可以很容易地嵌入Java的Swing和AWT。这个API的最新版本是Jexcel-2.6.12,发布于2009年,主要特点如下。

  • 自动化Excel应用程序,工作簿,工作表等
  • 在Java Swing应用程序作为普通的Swing组件嵌入到工作簿
  • 事件侦听器添加到工作簿和电子表格
  • 添加事件处理程序来处理的工作簿和电子表格事件的行为
  • 添加本地对等开发定制功能

Apache POI

Apache POI是Apache软件基金会提供的100%开源库。大多数中小规模的应用程序开发主要依赖于Apache POI(HSSF+ XSSF)。它支持Excel 库的所有基本功能; 然而,呈现和文本提取是它的主要特点。

Circle
 

Apache POI环境设置

本章将指导完成Apache POI在Windows和Linux系统为基础的设置过程。 Apache POI可以轻松地安装和集成,下面没有任何复杂的设置过程,通过几个简单步骤,目前Java环境,用户管理是必需安装的。

系统要求

JDK Java SE 2 JDK 1.5 或以上
内存 1 GB RAM (推荐)
磁盘空间 没有最小要求
操作系统版本 Windows XP 或以上, Linux

现在让我们继续安装Apache POI 的步骤。

第1步:验证Java安装

首先,需要在系统上安装Java软件开发工具包(SDK)。为了验证这一点,执行任何根据使用的平台上的以下两个命令。

如果Java安装已完成正确,那么它会显示Java安装的当前版本和规范。样本输出给下表中。

平台 命令 样本输出
Windows 打开命令控制台然后键入:

\>java –version

Java version "1.7.0_60" Java (TM) SE Run Time Environment (build 1.7.0_60-b19) Java Hotspot (TM) 64-bit Server VM (build 24.60-b09,mixed mode)

Linux 打开命令终端,输入: $java –version java version "1.7.0_25" Open JDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64) Open JDK 64-Bit Server VM (build 23.7-b01, mixed mode)

第2步:设置Java环境

设置环境变量JAVA_HOME指向安装了机器上Java的基本目录位置。例如,

平台 描述
Windows Set JAVA_HOME to C:\ProgramFiles\java\jdk1.7.0_60
Linux Export JAVA_HOME=/usr/local/java-current

添加Java编译器位置的完整路径到系统路径。

平台 描述
Windows Linux
添加字符串 "C:\Program Files\Java\jdk1.7.0_60\bin" 到系统环境变量PATH 的尾部。 Export PATH=$PATH:$JAVA_HOME/bin/

从命令提示符下执行命令java-version如上所述。

第3步:安装Apache POI库

从 http://poi.apache.org/download.htmll 下载Apache POI的最新版本,并解压缩所需要的库,可以链接到Java程序的文件夹。我们假设该文件在C盘的文件夹中。

下面的图像显示所下载的文件夹内的目录和文件结构。

JarJar

添加的五个jar文件的完整路径,在上面的图片路径到CLASSPATH。

平台 描述
Windows

添加以下字符串到用户变量的末尾

CLASSPATH:

“C:\poi-3.9\poi-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;”

“C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;”

“C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;.;”

Linux

Export CLASSPATH=$CLASSPATH:

/usr/share/poi-3.9/poi-3.9-20121203.tar:

/usr/share/poi-3.9/poi-ooxml-schemas-3.9-20121203.tar:

/usr/share/poi-3.9/poi-ooxml-3.9-20121203.tar:

/usr/share/poi-3.9/ooxml-lib/dom4j-1.6.1.tar:

/usr/share/poi-3.9/ooxml-lib/xmlbeans-2.3.0.tar

 
到这里,整个POI环境设置已经完成,下一节我们来学习如何使用。


POI核心类

本章介绍了Apache POI的API,它是至关重要的工作,使用Java程序操作Excel文件有下面几个类和方法。

工作簿

这是创建或维护Excel工作簿的所有类的超接口。它属于org.apache.poi.ss.usermodel包。是实现此接口的两个类,如下所示:

  • HSSFWorkbook : 这个类有读取和.xls 格式和写入Microsoft Excel文件的方法。它与微软Office97-2003版本兼容。

  • XSSFWorkbook : 这个类有读写Microsoft Excel和OpenOffice的XML文件的格式.xls或.xlsx的方法。它与MS-Office版本2007或更高版本兼容。

HSSFWorkbook

它是在org.apache.poi.hssf.usermodel包的高层次的类。它实现了Workbook 接口,用于Excel文件中的.xls格式。下面列出的是一些本类下的方法和构造函数。

类的构造函数

S.No. 构造函数和说明
1

HSSFWorkbook()

从头开始创建一个新的HSSFWorkbook对象时。

2

HSSFWorkbook(DirectoryNode directory, boolean preserveNodes)

创建一个特定的目录中一个新的HSSFWworkbook对象。

3

HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes)

给定一个POIFSFileSystem对象和特定的目录中,它创建了一个SSFWorkbook对象读取指定的工作簿。

4

HSSFWorkbook(java.io.InputStream s)

创建使用输入流中的新HSSFWorkbook对象时。

5

HSSFWorkbook(java.io.InputStream s, boolean preserveNodes)

构建在输入流的POI文件系统。

6

HSSFWorkbook(POIFSFileSystem fs)

使用POIFSFileSystem对象构造的新HSSFWorkbook对象时。

7

HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)

给定一个POIFSFileSystem对象时,它会创建一个新的HSSFWorkbook对象时读取指定的工作簿。

这些构造内的常用参数:

  • directory : 这是从POI文件系统处理的目录。

  • fs :它是包含簿流该POI的文件系统。

  • preservenodes : 这是决定是否保留其他节点像宏的可选参数。它消耗大量的内存,因为它存储在内存中的所有POIFileSystem(如果设置)。

注意:HSSFWorkbook类包含了许多方法;然而,它们仅与XLS格式兼容。在本教程中,重点是在Excel文件格式的最新版本。因此,HSSFWorkbook类的方法,这里没有列出。如果需要这些类的方法,那么请参照POI-HSSFWorkbook类API在 https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html.

XSSFWorkbook

它是用于表示高和低层次Excel文件格式的类。它属于org.apache.xssf.usemodel包,并实现Workbook接口。下面列出的是这个类的方法和构造函数。

类的构造函数

S.No. 构造函数和说明
1

XSSFWorkbook()

从头开始创建一个新的XSSFworkbook对象。

2

XSSFWorkbook(java.io.File file)

构造从给定文件中的XSSFWorkbook对象。

3

XSSFWorkbook(java.io.InputStream is)

构造一个XSSFWorkbook对象,通过缓冲整个输入流到内存中,然后为它打开一个OPCPackage对象。

4

XSSFWorkbook(java.lang.String path)

构建一个给定文件的完整路径的XSSFWorkbook对象。

类方法

S.No. 方法及描述
1

createSheet()

创建一个XSSFSheet本工作簿,将其添加到表,并返回高层表示。

2

createSheet(java.lang.String sheetname)

创建此工作簿的新表,并返回高层表示。

3

createFont()

创建一个新的字体,并将其添加到工作簿的字体表。

4

createCellStyle()

创建一个新的XSSFCellStyle并将其添加到工作簿的样式表。

5

createFont()

创建一个新的字体,并将其添加到工作簿的字体表。

6

setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow,int endRow)

设置一个给定的表按照指定参数的打印区域。

对于此类的其余的方法,请参阅完整的API文档:http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html. 列出了所有方法。

Sheet

Sheet是在org.apache.poi.ss.usermodel包的接口,它是创建具有特定名称的高或低级别的电子表格的所有类的超接口。电子表格的最常见的类型是工作表,它被表示为单元的网格。

HSSFSheet

这是在org.apache.poi.hssf.usermodel包的类。它可以创建Excel电子表格,它允许在sheet 方式和表数据格式。

类的构造函数

S.No. 构造函数及描述
1

HSSFSheet(HSSFWorkbook workbook)

创建新HSSFSheet通过调用HSSFWorkbook从头开始创建一个表。

2

HSSFSheet(HSSFWorkbook workbook, InternalSheet sheet)

创建HSSFSheet表示给定表对象。

XSSFSheet

这是代表了Excel电子表格的高级别代表的一类。这在org.apache.poi.hssf.usermodel包下。

类的构造函数

S.No. 构造函数及描述
1

XSSFSheet()

创造了新的XSSFSheet- 调用XSSFWorkbook从头开始创建一个表。

2

XSSFSheet(PackagePart part, PackageRelationship rel)

创建XSSFSheet表示给定包的一部分和关系。

类方法

S.No. 方法和描述
1

addMergedRegion(CellRangeAddress region)

添加单元的合并区域(因此这些单元格合并形成一个)。

2

autoSizeColumn(int column)

调整列宽,以适应的内容。

3

iterator()

此方法是用于rowIterator()的别名,以允许foreach循环

4

addHyperlink(XSSFHyperlink hyperlink)

注册超链接的集合中的超链接此工作表格上

对于此类的其余的方法,请参阅完整的API在:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html.

这是在org.apache.poi.ss.usermodel包的接口。它是用于一排的电子表格的高层表示。它是代表了POI库的行所有类的超接口。

XSSFRow

这是在org.apache.poi.xssf.usermodel包的类。它实现了Row接口,因此它可以在电子表格中创建行。下面列出的是这个类在方法和构造函数。

类方法

S.No. 描述
1

createCell(int columnIndex)

创建新单元行并返回。

2

setHeight(short height)

设置短单位的高度。

对于此类的其余的方法,参考如下链接:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html

单元格

这是在org.apache.poi.ss.usermodel包的接口。它是代表了单元在电子表格中的行中的所有类的超接口。

单元格可以使用各种属性,例如空白,数字,日期,错误等单元格被添加到一个行之前应具有(基于0)自己的编号。

XSSFCell

这是在 org.apache.poi.xssf.usermodel 包的类。它实现了单元格界面。它是单元在电子表格中的行的一个高层次的表示。

字段摘要

下面列出的是一些XSSFCell类的字段以及它们的描述。

单元格类型 描述
CELL_TYPE_BLANK 代表空白单元格
CELL_TYPE_BOOLEAN 代表布尔单元(true或false)
CELL_TYPE_ERROR 表示在单元的误差值
CELL_TYPE_FORMULA 表示一个单元格公式的结果
CELL_TYPE_NUMERIC 表示对一个单元的数字数据
CELL_TYPE_STRING 表示对一个单元串(文本)

类方法

S.No. 描述
1

setCellStyle(CellStyle style)

为单元格设置样式。

2

setCellType(int cellType)

设置单元格的类型(数字,公式或字符串)。

3

setCellValue(boolean value)

设置单元格一个布尔值

4

setCellValue(java.util.Calendar value)

设置一个日期值的单元格。

5

setCellValue(double value)

设置为单元格的数值。

6

setCellValue(java.lang.String str)

设置为单元格的字符串值。

7

setHyperlink(Hyperlink hyperlink)

分配超链接到该单元格。

对于这个类的剩余方法和字段,请访问以下链接查看详细:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCell.html

XSSFCellStyle

这是在org.apache.poi.xssf.usermodel包的类。它将提供关于在电子表格的单元格中的内容的格式可能的信息。它也提供了用于修正该格式的选项。它实现了CellStyle接口。

字段摘要

下表列出了从CellStyle接口继承一些字段。

字段名称 字段描述
ALIGN_CENTER 中心对齐单元格内容
ALIGN_CENTER_SELECTION 中心选择水平对齐方式
ALIGN_FILL 单元格适应于内容的大小
ALIGN_JUSTIFY 适应单元格内容的宽度
ALIGN_LEFT 左对齐单元格内容
ALIGN_RIGHT 右对齐单元格内容
BORDER_DASH_DOT 使用破折号和点单元格样式
BORDER_DOTTED 用虚线边框的单元格样式
BORDER_DASHED 用虚线边框的单元格样式
BORDER_THICK 厚厚的边框单元格样式
BORDER_THIN 薄边框的单元格样式
VERTICAL_BOTTOM 对齐单元格内容的垂直下方
VERTICAL_CENTER 对齐单元格内容垂直居中
VERTICAL_JUSTIFY 对齐和垂直对齐的单元格内容
VERTICAL_TOP 顶部对齐为垂直对齐

类的构造函数

S.No. 构造函数及描述
1

XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource, ThemesTable theme)

创建一个单元格样式,从所提供的部分

2

XSSFCellStyle(StylesTable stylesSource)

创建一个空的单元样式

类方法

设置边框的类型为单元格的底部边界

S.No 方法及描述
1

setAlignment(short align)

设置单元格为水平对齐的类型

2

setBorderBottom(short border)

3

setBorderColor(XSSFCellBorder.BorderSide side, XSSFColor color)

选定的边框颜色

4

setBorderLeft(Short border)

设置边界的类型单元格的左边框

5

setBorderRight(short border)

设置边框的类型为单元格的右边界

6

setBorderTop(short border)

设置边界的类型的单元上边框

7

setFillBackgroundColor(XSSFColor color)

设置表示为XSSFColor值背景填充颜色。

8

setFillForegroundColor(XSSFColor color)

设置表示为XSSFColor的值前景填充颜色。

9

setFillPattern(short fp)

指定单元格的填充信息模式和纯色填充单元。

10

setFont(Font font)

设置此样式的字体。

11

setRotation(short rotation)

设置的旋转为在单元格中文本的程度。

12

setVerticalAlignment(short align)

设置单元类型为垂直取向

对于这个类剩下的方法和字段,通过以下链接:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html

HSSFColor

这是在org.apache.poi.hssf.util包的类。它提供了不同的颜色作为嵌套类。通常这些嵌套类是使用自己的索引来表示。它实现了Color接口。

嵌套类

所有嵌套类这个类是静态的,每个类都有其索引。这些嵌套色类用于单元格格式,如单元格内容,边框,前景和背景。下面列出了一些的嵌套类。

S.No. 类名(颜色)
1 HSSFColor.AQUA
2 HSSFColor.AUTOMATIC
3 HSSFColor.BLACK
4 HSSFColor.BLUE
5 HSSFColor.BRIGHT_GREEN
6 HSSFColor.BRIGHT_GRAY
7 HSSFColor.CORAL
8 HSSFColor.DARK_BLUE
9 HSSFColor.DARK_GREEN
10 HSSFColor.SKY_BLUE
11 HSSFColor.WHITE
12 HSSFColor.YELLOW

类方法

这个类的只有一个方法是很重要的,并且用于获取索引值。

S.No. 方法和描述
1

getIndex()

这种方法被用来获得一个嵌套类的索引值

对于其余的方法和嵌套类,请参阅以下链接:
https://poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html.

XSSFColor

这是在org.apache.poi.xssf.usermodel包的类。它是用来表示在电子表格中的颜色。它实现了颜色的接口。下面列出的是它的一些方法和构造函数。

类的构造函数

S.No. Constructor and 描述
1

XSSFColor()

创建XSSFColor的新实例。

2

XSSFColor(byte[] rgb)

创建XSSFColor使用RGB的新实例。

3

XSSFColor(java.awt.Color clr)

创建XSSFColor使用Color类从AWT包的新实例。

类方法

S.No. 方法和描述
1

setAuto(boolean auto)

设置一个布尔值,表示ctColor是自动的,系统ctColor依赖。

2

setIndexed(int indexed)

设置索引ctColor值系统ctColor。

对于其余的方法,请访问以下链接:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFColor.html.

XSSFFont

这是在org.apache.poi.xssf.usermodel包的类。它实现了Font接口,因此它可以处理工作簿中不同的字体。

类的构造函数

S.No. 构造函数和描述
1

XSSFFont()

创建一个新的XSSFont实例。

类方法

S.No. 方法和描述
1

setBold(boolean bold)

设置“bold”属性的布尔值。

2

setColor(short color)

设置索引颜色的字体。

3

setColor(XSSFColor color)

设置为标准Alpha RGB颜色值的字体颜色。

4

setFontHeight(short height)

设置在点的字体高度。

5

setFontName(java.lang.String name)

设置字体的名称。

6

setItalic(boolean italic)

设置“italic”属性一个布尔值。

对于其余的方法,通过以下链接:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFFont.html.

XSSFHyperlink

这是在org.apache.poi.xssf.usermodel包的类。它实现了Hyperlink接口。它是用来连结设置为电子表格的单元格内容。

字段

属于此类的字段如下。这里,字段意味着使用超链接的类型。

字段 描述
LINK_DOCUMENT 用于连接任何其他文件
LINK_EMAIL 用于链接的电子邮件
LINK_FILE 用于以任何格式链接任何其他文件
LINK_URL 用来连接一个网页URL

类方法

S.No. 方法及描述
1

setAddress(java.lang.String address)

超链接地址。

对于其余的方法,请访问以下链接:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFHyperlink.html

XSSFCreationHelper

这是在org.apache.poi.xssf.usermodel包的类。它实现了CreationHelper接口。它被用作公式求值和设置超文本链接支持类。

类方法

S.No. 方法和描述
1

createFormulaEvaluator()

创建一个XSSFFormulaEvaluator例如,结果计算公式的单元格的对象。

2

createHyperlink(int type)

Creates a new XSSFHyperlink.

对于其余的方法,请参考以下链接:https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCreationHelper.html.

XSSFPrintSetup

这是在org.apache.poi.xsssf.usermodel包下的类。它实现了PrintSetup接口。它是用来设置打印页面大小,面积,选项和设置。

类方法

S.No. 方法及说明
1

setLandscape(boolean ls)

设置一个布尔值,允许或阻止横向打印。

2

setLeftToRight(boolean ltor)

设置是否向左走向右或自上而下的顺序,同时打印。

3

setPaperSize(short size)

设置纸张尺寸。

对于其余的方法,请访问以下链接:https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFPrintSetup.html

Apache POI工作簿

此处的术语“Workbook”指的Microsoft Excel文件。本章完成后,您将能够创建新的工作簿,并可以使用Java程序打开现有工作簿。

创建空白工作簿

下面简单的程序来创建一个空白Microsoft Excel工作簿。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.*;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.*;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">CreateWorkBook</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Create Blank workbook</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="com" style="color: rgb(136, 0, 0);">//Create file system using specific name</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"createworkbook.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//write operation workbook using file out object </span><span class="pln" style="color: rgb(0, 0, 0);">
      workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">write</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">close</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"
      createworkbook.xlsx written successfully"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

让我们保存上面的Java代码为CreateWorkBook.java,然后编译并从命令提示符如下执行它:

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">CreateWorkBook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">CreateWorkBook</span>

如果系统环境配置了POI 库,它会编译和执行,并生成一个名为createworkbook.xlsx 在当前目录下的空白Excel文件并显示在命令提示符处键入以下输出。

<span class="pln" style="color: rgb(0, 0, 0);">createworkbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xlsx written successfully</span>

打开现有工作簿

使用下面的代码打开现有的工作簿。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.*;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.*;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">OpenWorkBook</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">[])</span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pln" style="color: rgb(0, 0, 0);"> file </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"openworkbook.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">FileInputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> fIP </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileInputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">file</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Get the workbook instance for XLSX file </span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">fIP</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">file</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">isFile</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">&&</span><span class="pln" style="color: rgb(0, 0, 0);"> file</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">exists</span><span class="pun" style="color: rgb(102, 102, 0);">())</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="str" style="color: rgb(0, 136, 0);">"openworkbook.xlsx file open successfully."</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">else</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="str" style="color: rgb(0, 136, 0);">"Error to open openworkbook.xlsx file."</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

保存上面的Java代码为OpenWorkBook.java,然后编译并从命令提示符如下执行它:

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">OpenWorkBook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">OpenWorkBook</span>

这将编译和执行生成以下输出。

<span class="pln" style="color: rgb(0, 0, 0);">openworkbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xlsx file open successfully</span><span class="pun" style="color: rgb(102, 102, 0);">.</span>

打开工作簿后,可以进行读取,并在上面写操作。

Apache POI电子表格/Spreadsheet

本章将介绍如何创建一个电子表格,并使用Java操纵它。电子表格是在Excel文件中的页面;它包含具有特定名称的行和列。

读完本章后,将能够创建一个电子表格,并能在其上执行读取操作。

创建电子表格

首先,让我们创建一个使用在前面的章节中讨论的引用的类的电子表格。按照前面的章节中,首先创建一个工作簿,然后我们就可以去,并创建一个表。

下面的代码片段用于创建电子表格。

<span class="com" style="color: rgb(136, 0, 0);">//Create Blank workbook</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="com" style="color: rgb(136, 0, 0);">//Create a blank spreadsheet</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createSheet</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Sheet Name"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>

在电子表格的行

电子表格有一个网格布局。行和列被标识与特定的名称。该列标识字母和行用数字。

下面的代码片段用于创建一个行。

<span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pln" style="color: rgb(0, 0, 0);"> row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>

写入到电子表格

让我们考虑雇员数据的一个例子。这里的雇员数据给出以表格形式。

Emp Id Emp Name 称号
Tp01 Gopal Technical Manager
TP02 Manisha Proof Reader
Tp03 Masthan Technical Writer
Tp04 Satish Technical Writer
Tp05 Krishna Technical Writer

以下代码是用来写上述数据到电子表格。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Map</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Set</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">TreeMap</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ss</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Cell</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Writesheet</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Create blank workbook</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="com" style="color: rgb(136, 0, 0);">//Create a blank sheet</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createSheet</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">" Employee Info "</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Create row object</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//This data needs to be written (Object[])</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">Map</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> empinfo </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">TreeMap</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">>();</span><span class="pln" style="color: rgb(0, 0, 0);">
      empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">put</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"1"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"EMP ID"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"EMP NAME"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"DESIGNATION"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">});</span><span class="pln" style="color: rgb(0, 0, 0);">
      empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">put</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"2"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"tp01"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Gopal"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Technical Manager"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">});</span><span class="pln" style="color: rgb(0, 0, 0);">
      empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">put</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"3"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"tp02"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Manisha"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Proof Reader"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">});</span><span class="pln" style="color: rgb(0, 0, 0);">
      empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">put</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"4"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"tp03"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Masthan"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Technical Writer"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">});</span><span class="pln" style="color: rgb(0, 0, 0);">
      empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">put</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"5"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"tp04"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Satish"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Technical Writer"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">});</span><span class="pln" style="color: rgb(0, 0, 0);">
      empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">put</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"6"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"tp05"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Krishna"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"Technical Writer"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">});</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Iterate over data and write to sheet</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">Set</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> keyid </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">keySet</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln" style="color: rgb(0, 0, 0);"> rowid </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> key </span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> keyid</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
         row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">rowid</span><span class="pun" style="color: rgb(102, 102, 0);">++);</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> objectArr </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> empinfo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">get</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">key</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln" style="color: rgb(0, 0, 0);"> cellid </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">Object</span><span class="pln" style="color: rgb(0, 0, 0);"> obj </span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> objectArr</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
            </span><span class="typ" style="color: rgb(127, 0, 85);">Cell</span><span class="pln" style="color: rgb(0, 0, 0);"> cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">cellid</span><span class="pun" style="color: rgb(102, 102, 0);">++);</span><span class="pln" style="color: rgb(0, 0, 0);">
            cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);">obj</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Write the workbook in file system</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Writesheet.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
      workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">write</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">close</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"Writesheet.xlsx written successfully"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

上面的Java代码保存为Writesheet.java,然后并在命令提示符下编译运行,如下所示:

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">Writesheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">Writesheet</span>

这将编译和执行来生成一个Excel文件名为Writesheet.xlsx在当前目录中,在命令提示符处键入以下输出。

<span class="typ" style="color: rgb(127, 0, 85);">Writesheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xlsx written successfully</span>

Writesheet.xlsx文件的内容如下所示。

Writesheet

从电子表格读取数据

让我们考虑上述excel文件命名Writesheet.xslx作为输入文件。注意下面的代码;它是用于从电子表格中读取数据。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">FileInputStream</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Iterator</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ss</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Cell</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ss</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Row</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Readsheet</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">FileInputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> fis </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileInputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"WriteSheet.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">fis</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getSheetAt</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">Iterator</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Row</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> rowIterator </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">iterator</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">while</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">rowIterator</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">hasNext</span><span class="pun" style="color: rgb(102, 102, 0);">())</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
         row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> rowIterator</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">next</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="typ" style="color: rgb(127, 0, 85);">Iterator</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Cell</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> cellIterator </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">cellIterator</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="kwd" style="color: rgb(0, 0, 136);">while</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> cellIterator</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">hasNext</span><span class="pun" style="color: rgb(102, 102, 0);">())</span><span class="pln" style="color: rgb(0, 0, 0);"> 
         </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
            </span><span class="typ" style="color: rgb(127, 0, 85);">Cell</span><span class="pln" style="color: rgb(0, 0, 0);"> cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> cellIterator</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">next</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
            </span><span class="kwd" style="color: rgb(0, 0, 136);">switch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getCellType</span><span class="pun" style="color: rgb(102, 102, 0);">())</span><span class="pln" style="color: rgb(0, 0, 0);"> 
            </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
               </span><span class="kwd" style="color: rgb(0, 0, 136);">case</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">CELL_TYPE_NUMERIC</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);">
               </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">print</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
               cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getNumericCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">" \t\t "</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
               </span><span class="kwd" style="color: rgb(0, 0, 136);">break</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
               </span><span class="kwd" style="color: rgb(0, 0, 136);">case</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">CELL_TYPE_STRING</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);">
               </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">print</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
               cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getStringCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">" \t\t "</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
               </span><span class="kwd" style="color: rgb(0, 0, 136);">break</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
            </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
         </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
      fis</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">close</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

让我们把上面的代码保存在Readsheet.java文件,然后编译并在命令提示符下运行,如下所示:

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">Readsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">Readsheet</span>

如果您的系统环境配置了POI库,它会编译和执行产生在命令提示符处键入以下输出。

<span class="pln" style="color: rgb(0, 0, 0);">EMP ID EMP NAME DESIGNATION 
 tp01   </span><span class="typ" style="color: rgb(127, 0, 85);">Gopal</span><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(127, 0, 85);">Technical</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Manager</span><span class="pln" style="color: rgb(0, 0, 0);"> 
 tp02   </span><span class="typ" style="color: rgb(127, 0, 85);">Manisha</span><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(127, 0, 85);">Proof</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Reader</span><span class="pln" style="color: rgb(0, 0, 0);"> 
 tp03   </span><span class="typ" style="color: rgb(127, 0, 85);">Masthan</span><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(127, 0, 85);">Technical</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Writer</span><span class="pln" style="color: rgb(0, 0, 0);"> 
 tp04   </span><span class="typ" style="color: rgb(127, 0, 85);">Satish</span><span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="typ" style="color: rgb(127, 0, 85);">Technical</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Writer</span><span class="pln" style="color: rgb(0, 0, 0);"> 
 tp05   </span><span class="typ" style="color: rgb(127, 0, 85);">Krishna</span><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(127, 0, 85);">Technical</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Writer</span>

Apache POI单元格/Cells

输入到电子表格中的任何数据总是存储在一个单元中。我们使用的行和列的标签来识别单元格。本章介绍了如何使用Java编程操纵单元电子表格的数据。

创建一个单元格

需要创建一个单元之前创建一个行。行是什么?只不过是单元的集合。

下面的代码片段用于创建一个单元格。

<span class="com" style="color: rgb(136, 0, 0);">//create new workbook</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="com" style="color: rgb(136, 0, 0);">//create spreadsheet with a name</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createSheet</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"new sheet"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="com" style="color: rgb(136, 0, 0);">//create first row on a created spreadsheet</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pln" style="color: rgb(0, 0, 0);"> row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="com" style="color: rgb(136, 0, 0);">//create first cell on created row</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pln" style="color: rgb(0, 0, 0);"> cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>

单元格类型

单元格类型指定单元格是否可以包含字符串,数值,或公式。字符串单元不能持有数值和数值单元格无法容纳字符串。下面给出是单元格值和类型的语法。

单元格的值类型 类型语法
Blank cell value XSSFCell.CELL_TYPE_BLANK
Boolean cell value XSSFCell.CELL.TYPE_BOOLEAN
Error cell value XSSFCell.CELL_TYPE_ERROR
Numeric cell value XSSFCell.CELL_TYPE_NUMERIC
String cell value XSSFCell.CELL_TYPE_STRING

以下代码是用于在电子表格创建不同类型的单元格。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Date</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">TypesofCells</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createSheet</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"cell types"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pln" style="color: rgb(0, 0, 0);"> row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Type of Cell"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"cell value"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"set cell type BLANK"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">4</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"set cell type BOOLEAN"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">true</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"set cell type ERROR"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">CELL_TYPE_ERROR </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">6</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"set cell type date"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Date</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">7</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"set cell type numeric"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">20</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">8</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"set cell type string"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"A String"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"typesofcells.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
      workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">write</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">close</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="str" style="color: rgb(0, 136, 0);">"typesofcells.xlsx written successfully"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

保存上面的代码到一个名为TypesofCells.java文件,编译并从命令提示符如下执行它。

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">TypesofCells</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">TypesofCells</span>

如果您的系统配置了POI库,那么它会编译和执行在当前目录中生成一个名为typesofcells.xlsx的Excel文件,并显示以下输出。

<span class="pln" style="color: rgb(0, 0, 0);">typesofcells</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xlsx written successfully</span>

typesofcells.xlsx文件如下所示。

Type Of Cells

单元格样式

在这里,可以学习如何做单元格格式,并采用不同的风格,如合并相邻的单元格,添加边框,设置单元格对齐方式和填充颜色。

以下代码是使用Java编程用于不同样式应用到单元格。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">hssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">HSSFColor</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ss</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">IndexedColors</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ss</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">CellRangeAddress</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">CellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createSheet</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"cellstyle"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pln" style="color: rgb(0, 0, 0);"> row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setHeight</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">800</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pln" style="color: rgb(0, 0, 0);"> cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"test of merging"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//MEARGING CELLS </span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//this statement for merging cells</span><span class="pln" style="color: rgb(0, 0, 0);">
      spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">addMergedRegion</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">CellRangeAddress</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//first row (0-based)</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//last row (0-based)</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//first column (0-based)</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">4</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//last column (0-based)</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//CELL Alignment</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setHeight</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">800</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">// Top Left alignment </span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> style1 </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setColumnWidth</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">8000</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ALIGN_LEFT</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setVerticalAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">VERTICAL_TOP</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Top Left"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">style1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">6</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setHeight</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">800</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">// Center Align Cell Contents </span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> style2 </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      style2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ALIGN_CENTER</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setVerticalAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">VERTICAL_CENTER</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Center Aligned"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">style2</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">7</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setHeight</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">800</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">// Bottom Right alignment </span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> style3 </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      style3</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ALIGN_RIGHT</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style3</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setVerticalAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">VERTICAL_BOTTOM</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Bottom Right"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">style3</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">8</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">// Justified Alignment </span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> style4 </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      style4</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ALIGN_JUSTIFY</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style4</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setVerticalAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">VERTICAL_JUSTIFY</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Contents are Justified in Alignment"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">style4</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//CELL BORDER</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setHeight</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">800</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"BORDER"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> style5 </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setBorderBottom</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">BORDER_THICK</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setBottomBorderColor</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">IndexedColors</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">BLUE</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getIndex</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setBorderLeft</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">BORDER_DOUBLE</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setLeftBorderColor</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">IndexedColors</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">GREEN</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getIndex</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setBorderRight</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">BORDER_HAIR</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setRightBorderColor</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">IndexedColors</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">RED</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getIndex</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setBorderTop</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">BIG_SPOTS</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style5</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setTopBorderColor</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">IndexedColors</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">CORAL</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getIndex</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">style5</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Fill Colors</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//background color</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> style6 </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      style6</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setFillBackgroundColor</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">HSSFColor</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">LEMON_CHIFFON</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">index </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style6</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setFillPattern</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">LESS_DOTS</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style6</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ALIGN_FILL</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setColumnWidth</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="lit" style="color: rgb(0, 102, 102);">8000</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"FILL BACKGROUNG/FILL PATTERN"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">style6</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//Foreground color</span><span class="pln" style="color: rgb(0, 0, 0);">
      row </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">12</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="kwd" style="color: rgb(0, 0, 136);">short</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pln" style="color: rgb(0, 0, 0);"> style7</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      style7</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setFillForegroundColor</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">HSSFColor</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">BLUE</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">index</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style7</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setFillPattern</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">LESS_DOTS</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      style7</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAlignment</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ALIGN_FILL</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"FILL FOREGROUND/FILL PATTERN"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">style7</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"cellstyle.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
      workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">write</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">close</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"cellstyle.xlsx written successfully"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

保存上面的代码在一个名为CellStyle.java文件,编译并从命令提示符如下执行它。

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">CellStyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">CellStyle</span>

它会生成一个名为cellstyle.xlsx在当前目录中的Excel文件并显示以下输出。

<span class="pln" style="color: rgb(0, 0, 0);">cellstyle</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xlsx written successfully</span>

cellstyle.xlsx文件如下所示。

CellStyle
 

Apache POI字体/Fonts

本章介绍如何设置不同的字体,应用样式,并在Excel电子表格中显示的方向不同角度的文字。

每个系统附带一个很大的字体如 Arial, Impact, Times New Roman,等字体集合也可以用新的字体更新,如果需要的话。同样也有各种风格,其中的字体可以显示,例如,粗体,斜体,下划线,删除线等。

字体和字体样式

下面的代码用于特定的字体和样式应用于一单元格的内容。

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class FontStyle 
{
   public static void main(String[] args)throws Exception 
   {
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook.createSheet("Fontstyle");
      XSSFRow row = spreadsheet.createRow(2);
      //Create a new font and alter it.
      XSSFFont font = workbook.createFont();
      font.setFontHeightInPoints((short) 30);
      font.setFontName("IMPACT");
      font.setItalic(true);
      font.setColor(HSSFColor.BRIGHT_GREEN.index);
      //Set font into style
      XSSFCellStyle style = workbook.createCellStyle();
      style.setFont(font);
      // Create a cell with a value and set style to it.
      XSSFCell cell = row.createCell(1);
      cell.setCellValue("Font Style");
      cell.setCellStyle(style);
      FileOutputStream out = new FileOutputStream(
      new File("fontstyle.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println(
      "fontstyle.xlsx written successfully");
   }
}

让我们保存上面的代码在一个名为FontStyle.java文件。从命令提示符编译并执行它如下。

$javac FontStyle.java
$java FontStyle

它生成一个名为fontstyle.xlsx在当前目录中的Excel文件并显示在命令提示符处键入以下输出。

fontstyle.xlsx written successfully

fontstyle.xlsx文件如下所示。

FontStyle

文字方向

在这里,可以学习如何设置不同角度的文本方向。通常单元格的内容以水平方式显示,由左到右,并在00角;但是可以使用下面的代码来旋转文本的方向(如果需要的话)。

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TextDirection 
{
   public static void main(String[] args)throws Exception 
   {
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook.createSheet(
      "Text direction");
      XSSFRow row = spreadsheet.createRow(2);
      XSSFCellStyle myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 0);
      XSSFCell cell = row.createCell(1);
      cell.setCellValue("0D angle");
      cell.setCellStyle(myStyle);
      //30 degrees
      myStyle=workbook.createCellStyle();
      myStyle.setRotation((short) 30);
      cell = row.createCell(3);
      cell.setCellValue("30D angle");
      cell.setCellStyle(myStyle);
      //90 degrees
      myStyle=workbook.createCellStyle();
      myStyle.setRotation((short) 90);
      cell = row.createCell(5);
      cell.setCellValue("90D angle");
      cell.setCellStyle(myStyle);
      //120 degrees
      myStyle=workbook.createCellStyle();
      myStyle.setRotation((short) 120);
      cell = row.createCell(7);
      cell.setCellValue("120D angle");
      cell.setCellStyle(myStyle);
      //270 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 270);
      cell = row.createCell(9);
      cell.setCellValue("270D angle");
      cell.setCellStyle(myStyle);
      //360 degrees
      myStyle=workbook.createCellStyle();
      myStyle.setRotation((short) 360);
      cell = row.createCell(12);
      cell.setCellValue("360D angle");
      cell.setCellStyle(myStyle);
      FileOutputStream out = new FileOutputStream(
      new File("textdirection.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println( 
      "textdirection.xlsx written successfully");
   }
}

保持TextDirectin.java文件上面的代码,然后编译并从命令提示符如下执行它。

$javac TextDirection.java
$java TextDirection

这将编译和执行,以生成一个名为textdirection.xlsx在当前目录中的Excel文件并显示在命令提示符处键入以下输出。

textdirection.xlsx written successfully

textdirection.xlsx文件如下所示。

TextDirectin
 

Apache POI公式

本章将介绍如何使用Java编程应用不同单元公式的过程。 Excel应用程序的基本目的是通过应用公式就可以保持数值数据。

在公式中,我们通过动态值,或在Excel工作表中的值的位置。在执行这个公式,就会得到想要的结果。下表列出了常用的在Excel中的几个基本公式。

操作 语法
添加多个数值 = SUM(Loc1:Locn) or = SUM(n1,n2,)
计数 = COUNT(Loc1:Locn) or = COUNT(n1,n2,)
两个数的幂 = POWER(Loc1,Loc2) or = POWER(number, power)
多个数的最大值 = MAX(Loc1:Locn) or = MAX(n1,n2,)
乘积 = PRODUCT(Loc1:Locn) or = PRODUCT(n1,n2,)
阶乘 = FACT(Locn) or = FACT(number)
绝对数字 = ABS(Locn) or = ABS(number)
今天的日期 =TODAY()
转换成小写 = LOWER(Locn) or = LOWER(text)
平方根 = SQRT(locn) or = SQRT(number)

以下代码用于公式添加至单元格,并执行它。

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Formula 
{
   public static void main(String[] args)throws Exception 
   {
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook.createSheet("formula");
      XSSFRow row = spreadsheet.createRow(1);
      XSSFCell cell = row.createCell(1);
      cell.setCellValue("A =" );
      cell = row.createCell(2);
      cell.setCellValue(2);
      row = spreadsheet.createRow(2);
      cell = row.createCell(1);
      cell.setCellValue("B =");
      cell = row.createCell(2);
      cell.setCellValue(4);
      row = spreadsheet.createRow(3);
      cell = row.createCell(1);
      cell.setCellValue("Total =");
      cell = row.createCell(2);
      // Create SUM formula
      cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
      cell.setCellFormula("SUM(C2:C3)" );
      cell = row.createCell(3);
      cell.setCellValue("SUM(C2:C3)");
      row = spreadsheet.createRow(4);
      cell = row.createCell(1);
      cell.setCellValue("POWER =");
      cell=row.createCell(2);
      // Create POWER formula
      cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
      cell.setCellFormula("POWER(C2,C3)");
      cell = row.createCell(3);
      cell.setCellValue("POWER(C2,C3)");
      row = spreadsheet.createRow(5);
      cell = row.createCell(1);
      cell.setCellValue("MAX =");
      cell = row.createCell(2);
      // Create MAX formula
      cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
      cell.setCellFormula("MAX(C2,C3)");
      cell = row.createCell(3);
      cell.setCellValue("MAX(C2,C3)");
      row = spreadsheet.createRow(6);
      cell = row.createCell(1);
      cell.setCellValue("FACT =");
      cell = row.createCell(2);
      // Create FACT formula
      cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
      cell.setCellFormula("FACT(C3)");
      cell = row.createCell(3);
      cell.setCellValue("FACT(C3)");
      row = spreadsheet.createRow(7);
      cell = row.createCell(1);
      cell.setCellValue("SQRT =");
      cell = row.createCell(2);
      // Create SQRT formula
      cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
      cell.setCellFormula("SQRT(C5)");
      cell = row.createCell(3);
      cell.setCellValue("SQRT(C5)");
      workbook.getCreationHelper()
      .createFormulaEvaluator()
      .evaluateAll();
      FileOutputStream out = new FileOutputStream(
      new File("formula.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println("fromula.xlsx written successfully");
   }
}

保存上面的代码到文件Formula.java,然后编译并从命令提示符如下执行它。

$javac Formula.java
$java Formula

它会生成一个名为formula.xlsx在当前目录中的Excel文件并显示在命令提示符处键入以下输出。

fromula.xlsx written successfully

formula.xlsx文件如下所示。

Formula
 

Apache POI超链接

本章介绍了如何为超链接添加到内容的单元格。超链接通常被用来访问任何网站的网址,电子邮件或外部文件。

下面的代码演示如何创建单元格的超链接。

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class HyperlinkEX 
{
   public static void main(String[] args) throws Exception 
   {
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook
      .createSheet("Hyperlinks");
      XSSFCell cell;
      CreationHelper createHelper = workbook
      .getCreationHelper();
      XSSFCellStyle hlinkstyle = workbook.createCellStyle();
      XSSFFont hlinkfont = workbook.createFont();
      hlinkfont.setUnderline(XSSFFont.U_SINGLE);
      hlinkfont.setColor(HSSFColor.BLUE.index);
      hlinkstyle.setFont(hlinkfont);
      //URL Link
      cell = spreadsheet.createRow(1)
      .createCell((short) 1);
      cell.setCellValue("URL Link");
      XSSFHyperlink link = (XSSFHyperlink)createHelper
      .createHyperlink(Hyperlink.LINK_URL);
      link.setAddress("http://www.yiibai.com/" );
      cell.setHyperlink((XSSFHyperlink) link);
      cell.setCellStyle(hlinkstyle);
      //Hyperlink to a file in the current directory
      cell = spreadsheet.createRow(2)
      .createCell((short) 1);
      cell.setCellValue("File Link");
      link = (XSSFHyperlink)createHelper
      .createHyperlink(Hyperlink.LINK_FILE);
      link.setAddress("cellstyle.xlsx");
      cell.setHyperlink(link);
      cell.setCellStyle(hlinkstyle);
      //e-mail link
      cell = spreadsheet.createRow(3)
      .createCell((short) 1);
      cell.setCellValue("Email Link");
      link = (XSSFHyperlink)createHelper
      .createHyperlink(Hyperlink.LINK_EMAIL);
      link.setAddress( 
      "mailto:contact@yiibai.com?"
      +"subject=Hyperlink");
      cell.setHyperlink(link);
      cell.setCellStyle(hlinkstyle);
      FileOutputStream out = new FileOutputStream(
      new File("hyperlink.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println("hyperlink.xlsx written successfully");
   }
}

保存上面的代码到文件HyperlinkEX.java。并从命令提示符编译执行它如下。

$javac HyperlinkEX.java
$java HyperlinkEX

它会生成一个名为hyperlink.xlsx在当前目录中的Excel文件并显示在命令提示符处输出:

hyperlink.xlsx written successfully

hyperlink.xlsx文件如下所示。

Hyperlink
 

Apache POI打印区域

本章介绍了如何在电子表格中设置打印区域。通常打印区域从左上角到Excel电子表格右下角。打印区域可根据要求进行定制。它意味着可以从整个电子表格打印单元的特定范围,自定义的纸张大小,用网格线打印的内容接通等

以下代码是用来在电子表格中设置打印区域。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFPrintSetup</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">PrintArea</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook
      </span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createSheet</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"Print Area"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//set print area with indexes</span><span class="pln" style="color: rgb(0, 0, 0);">
      workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setPrintArea</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//sheet index</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//start column</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//end column</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//start row</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//end row</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//set paper size</span><span class="pln" style="color: rgb(0, 0, 0);">
      spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getPrintSetup</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">setPaperSize</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFPrintSetup</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">A4_PAPERSIZE</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="com" style="color: rgb(136, 0, 0);">//set display grid lines or not</span><span class="pln" style="color: rgb(0, 0, 0);">
      spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setDisplayGridlines</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">true</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
     </span><span class="com" style="color: rgb(136, 0, 0);">//set print grid lines or not</span><span class="pln" style="color: rgb(0, 0, 0);">
     spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setPrintGridlines</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">true</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
     </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
     </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"printarea.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
     workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">write</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
     </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">close</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
     </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"printarea.xlsx written successfully"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

让我们保存了上面的代码为PrintArea.java。编译并从命令提示符执行它如下。

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">PrintArea</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">PrintArea</span>

它会生成一个名为printarea.xlsx在当前目录下的文件,并显示在命令提示符处输出以下。

<span class="pln" style="color: rgb(0, 0, 0);">printarea</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xlsx written successfully</span>

在上面的代码中,我们还没有添加任何单元格值。因此printarea.xlsx是一个空白文件。但是可以在下图的打印预览显示网格线打印区域查看。

PrintArea
 

Apache POI数据库

本章介绍了POI库与数据库的交互方式。有了JDBC帮助,可以从数据库中检索数据并插入数据来使用POI库电子表格。让我们考虑SQL操作MySQL数据库。

写入数据库

让我们假设数据表是 emp_tbl 存有雇员信息是从MySQL数据库 test 中检索。

EMP ID EMP NAME DEG SALARY DEPT
1201 Gopal Technical Manager 45000 IT
1202 Manisha Proof reader 45000 Testing
1203 Masthanvali Technical Writer 45000 IT
1204 Kiran Hr Admin 40000 HR
1205 Kranthi Op Admin 30000

使用下面的代码从数据库中检索数据,并插入到同一个电子表格。

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sql</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Connection</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sql</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">DriverManager</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sql</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">ResultSet</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sql</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">Statement</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">apache</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">poi</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xssf</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">usermodel</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">ExcelDatabase</span><span class="pln" style="color: rgb(0, 0, 0);"> 
</span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(127, 0, 85);">String</span><span class="pun" style="color: rgb(102, 102, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> 
   </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">Class</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">forName</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"com.mysql.jdbc.Driver"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">Connection</span><span class="pln" style="color: rgb(0, 0, 0);"> connect </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">DriverManager</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getConnection</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"jdbc:mysql://localhost:3306/test"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"root"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="str" style="color: rgb(0, 136, 0);">"root"</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">Statement</span><span class="pln" style="color: rgb(0, 0, 0);"> statement </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> connect</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createStatement</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">ResultSet</span><span class="pln" style="color: rgb(0, 0, 0);"> resultSet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> statement
      </span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">executeQuery</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"select * from emp_tbl"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFWorkbook</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> 
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFSheet</span><span class="pln" style="color: rgb(0, 0, 0);"> spreadsheet </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> workbook
      </span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createSheet</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"employe db"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFRow</span><span class="pln" style="color: rgb(0, 0, 0);"> row</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">XSSFCell</span><span class="pln" style="color: rgb(0, 0, 0);"> cell</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"EMP ID"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"EMP NAME"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"DEG"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">4</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"SALARY"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"DEPT"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln" style="color: rgb(0, 0, 0);"> i</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">while</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">resultSet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">next</span><span class="pun" style="color: rgb(102, 102, 0);">())</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);">
         row</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">spreadsheet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createRow</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">i</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">resultSet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getInt</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"eid"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">resultSet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getString</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"ename"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">resultSet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getString</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"deg"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">4</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">resultSet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getString</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"salary"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">row</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createCell</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
         cell</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCellValue</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">resultSet</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getString</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"dept"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
         i</span><span class="pun" style="color: rgb(102, 102, 0);">++;</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">FileOutputStream</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(127, 0, 85);">File</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"exceldatabase.xlsx"</span><span class="pun" style="color: rgb(102, 102, 0);">));</span><span class="pln" style="color: rgb(0, 0, 0);">
      workbook</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">write</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">close</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="typ" style="color: rgb(127, 0, 85);">System</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">out</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">
      </span><span class="str" style="color: rgb(0, 136, 0);">"exceldatabase.xlsx written successfully"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">
   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

让我们保存了上面的代码为ExcelDatabase.java。编译并从命令提示符执行它如下。

<span class="pln" style="color: rgb(0, 0, 0);">$javac </span><span class="typ" style="color: rgb(127, 0, 85);">ExcelDatabase</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">java
$java </span><span class="typ" style="color: rgb(127, 0, 85);">ExcelDatabase</span>

它会生成一个名为exceldatabase.xlsx在当前目录中的Excel文件并显示在命令提示符处输出以下。

<span class="pln" style="color: rgb(0, 0, 0);">exceldatabase</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xlsx written successfully</span>

exceldatabase.xlsx文件如下所示。

Excel Database
posted on 2015-05-26 20:02  LinkinPark  阅读(706)  评论(0编辑  收藏  举报