乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>java</em>操作<em>excel</em>有多少个工具类

<em>java</em>操作<em>excel</em>有多少个工具类

作者:乔山办公网日期:

返回目录:excel表格制作


可以用GCExcel这个组件,GCExcel涵盖了目前几乎所有的Excel基本操作,并且可以无损导入/导出 Excel 文件,包括数据透视表、图表、注释、条件格式、数据验证、公式、形状、图片、迷你图和表格,完全参照Excel的规范。适用于所有 Java 6.0 及以上标准的平台,无需 Microsoft Excel 依赖 ,即可快速批量操作 Excel 文件。支持中文并且有中文的学习资源。

网页链接



JAVA 通常有两种方法来操作Excel,分别是POI和JExcelAPI,而且都是开源的。POI是Apace公司开发的,对中文的支持比较弱一些;而JExcelAPI是韩国公司开发的,不仅对中文的支持好,而且由于是纯JAVA编写的,所以可以跨平台操作。本文介绍的也是JExcelAPI的使用方法。

1、环境配置

如下网址,可以下载到API:e68a847a64366http://www.andykhan.com/jexcelapi/download.html

下载完成的包解压之后,可以得到如下几个重要的文件:

(1)jxl.jar —— JExcelAPI 函数库;

(2)docs —— 帮助文档;

(3)src —— 源码文件夹;

将jxl.jar复制到%JAVA_HOME%\jre\ext\文件夹下面,在CLASSPATH变量里面添加"%JAVA_HOME%\jre\ext",然后就可以调用JExcelAPI了。如果出现编译报错“找不到java.jxl包”,则可能是没有设置成功。这时,如果有Eclipse开发工具,可以在"Build Path"中添加"External Library",找到jxl.jar的路径,然后就能编译成功了。

2、Excel基础操作实例

(1) 创建Excel文件

/**读取Excel文件的内容
* @param file 待读取的文件
* @return // 生成Excel的类 */
package createxls;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class CreateXLS {
public static void main(String args[]) {
try {
// 打开文件
WritableWorkbook book = Workbook.createWorkbook( new File( " test.xls " ));
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet( " 第一页 " , 0 );
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
Label label = new Label( 0 , 0 , " test " );

// 将定义好的单元格添加到工作表中
sheet.addCell(label);

// 写入数据并关闭文件
book.write();
book.close();

} catch (Exception e) {
System.out.println(e);
}
}
}

(2)读Excel文件

package readxls;

//读取Excel的类
import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ReadXLS {
public static void main(String args[]) {
try {
Workbook book = Workbook.getWorkbook( new File( " test.xls " ));
// 获得第一个工作表对象
Sheet sheet = book.getSheet( 0 );
// 得到第一列第一行的单元格
Cell cell1 = sheet.getCell( 0 , 0 );
String result = cell1.getContents();
System.out.println(result);
book.close();
} catch (Exception e) {
//System.out.println(e);
e.printStackTrace();
}
}
}

(3)合并单元格、格式化单元格等

//合并单元格并在单元格中输入内容

package additionalproperty;

import java.io.*;
import jxl.write.*;
import jxl.*;

public class MergeCells {
public static void main(String [] args){
try{
WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"));
WritableSheet sheet = book.createSheet("第一页", 0);
sheet.mergeCells(3, 3, 6, 6); //合并单元格

//设置填充内容的格式
WritableFont font = new WritableFont(WritableFont.TIMES, 30, WritableFont.BOLD);
WritableCellFormat format = new WritableCellFormat(font);
format.setAlignment(jxl.format.Alignment.CENTRE); //水平居中
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中
format.setBackground(jxl.format.Colour.BLUE); //背景颜色和样式
format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THICK); //边框样式

Label label = new Label(3, 3, "合并", format); //添加内容
sheet.addCell(label);
book.write();
book.close();
}//end try
catch (Exception e){
e.printStackTrace();
}
}
}

(4)添加图片

JExcelAPI对图片的操作有限:它不能生成图表、图片和宏,但是复制工作表时,这些信息可以保留复制。而且当向工作表中添加图片时,只能支持PNG格式的图片。

//在工作表中添加图片

package handleimage;

import java.io.*;
import jxl.*;
import jxl.write.*;

public class CreateImage {
public static void main(String [] args){
try{
WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"));
WritableSheet sheet = book.createSheet("第一页", 0);
WritableImage image = new WritableImage(3.5, 3.5, 4.3, 8.7, //定义图片格式
new File("C:\\Documents and Settings\\Wei Li\\My Documents\\My Pictures\\Water lilies.PNG"));
sheet.addImage(image); //添加图片

book.write();
book.close();
}//end try
catch (Exception e){ e.printStackTrace(); }
}
}
package com.b2bjy.crm.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class POIExcelUtil
{
/** *//** 总行数 */
private int totalRows = 0;

/** *//** 总列数 */
private int totalCells = 0;

/** *//** 构造方法 */
public POIExcelUtil()
{}

/** *//**
* <ul>
* <li>Description:[根据文件名读取excel文件]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param fileName
* @return
* @throws Exception
*/
public ArrayList<String> read(String fileName)
{
ArrayList<String> dataLst = new ArrayList<String>();

/** *//** 检查文件名是否为空或者是否是Excel格式的文件 */
if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
{
return dataLst;
}

boolean isExcel2003 = true;
/** *//** 对文件的合法性进行验证 */
if (fileName.matches("^.+\\.(?i)(xlsx)$"))
{
isExcel2003 = false;
}

/** *//** 检查文件是否存在 */
File file = new File(fileName);
if (file == null || !file.exists())
{
return dataLst;
}

try
{
/** *//** 调用本类提供的根据流读取的方法 */
dataLst = read(new FileInputStream(file), isExcel2003);
}
catch (Exception ex)
{
ex.printStackTrace();
}

/** *//** 返回最后读取的结果 */
return dataLst;
}

/** *//**
* <ul>
* <li>Description:[根据流读取Excel文件]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param inputStream
* @param isExcel2003
* @return
*/
public ArrayList<String> read(InputStream inputStream,
boolean isExcel2003)
{
ArrayList<String> dataLst = null;
try
{
/** *//** 根据版本选择创建Workbook的方式 */
Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)
: new XSSFWorkbook(inputStream);
dataLst = read(wb);
}
catch (IOException e)
{
e.printStackTrace();
}
return dataLst;
}

/** *//**
* <ul>
* <li>Description:[得到总行数]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @return
*/
public int getTotalRows()
{
return totalRows;
}

/** *//**
* <ul>
* <li>Description:[得到总列数]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @return
*/
public int getTotalCells()
{
return totalCells;
}

/** *//**
* <ul>
* <li>Description:[读取数据]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param wb
* @return
*/
private ArrayList<String> read(Workbook wb)
{
ArrayList<String> rowLst = new ArrayList<String>();
/** *//** 得到第一个shell */
Sheet sheet = wb.getSheetAt(0);
this.totalRows = sheet.getPhysicalNumberOfRows();
if (this.totalRows >= 1 && sheet.getRow(0) != null)
{
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}

/** *//** 循环7a64e4b893e5b19e336Excel的行 */
for (int r = 0; r < this.totalRows; r++)
{
Row row = sheet.getRow(r);
if (row == null)
{
continue;
}

/** *//** 循环Excel的列 */
for (short c = 0; c < 1; c++)
{
Cell cell = row.getCell(c);
String cellValue = "";
if (cell == null)
{
rowLst.add(cellValue);
continue;
}

/** *//** 处理数字型的,自动去零 */
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType())
{
cellValue = getRightStr(cell.getNumericCellValue() + "");

}
/** *//** 处理字符串型 */
else if (Cell.CELL_TYPE_STRING == cell.getCellType())
{
cellValue = cell.getStringCellValue();
}
/** *//** 处理布尔型 */
else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType())
{
cellValue = cell.getBooleanCellValue() + "";
}
/** *//** 其它的,非以上几种数据类型 */
else
{
cellValue = cell.toString() + "";
}

rowLst.add(cellValue);
}
}
return rowLst;
}

/** *//**
* <ul>
* <li>Description:[正确地处理整数后自动加零的情况]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param sNum
* @return
*/
private String getRightStr(String sNum)
{
DecimalFormat decimalFormat = new DecimalFormat("#.000000");
String resultStr = decimalFormat.format(new Double(sNum));
if (resultStr.matches("^[-+]?\\d+\\.[0]+$"))
{
resultStr = resultStr.substring(0, resultStr.indexOf("."));
}
return resultStr;
}

/** *//**
* <ul>
* <li>Description:[测试main方法]</li>
* <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
* <li>Midified by [modifier] [modified time]</li>
* <ul>
*
* @param args
* @throws Exception
*/
public String getPhones(){

ArrayList<String> dataLst = new POIExcelUtil().read("D:\\b.xls");

StringBuffer rowData = new StringBuffer();
for (int i = 0; i < dataLst.size()-1; i++) {
rowData.append(dataLst.get(i)).append(",");
}
rowData.append(dataLst.get(dataLst.size()-1));
if (rowData.length() > 0)
{
System.out.println(rowData);
}
return rowData.toString();

}
public static void main(String[] args) throws Exception
{

System.out.println(new POIExcelUtil().getPhones());

}
}

我觉得用JAVA操作EXCEL的话,有点大刀小用,你说的这个东西其实很zhidao简单,EXCEL本身就是处理数据问题的。不知道你EXCEL水平怎么样,有几个思路我简单写下,希望可以帮到你:
1.如果地址是规范的(同一地址同一称为,不会有北京、北京市、北京海淀等)。排序后可以直接排序,增加辅助列。这是最基本最笨的办法。
2.VBA编程,实现的方法就很多了。

既然是模拟,就不帮你做了。自己多想想,也正好练习下EXCEL。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网