- 浏览: 95783 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
zyq873002362:
很不错的资源
DB2 SQLCODE 大全 -
tsl2122342:
能提供详细的解决办法吗?我正好遇到了这个问题,不想换解析器了, ...
Sax 解析xml文件时, 数据信息不完整问题解决方法 -
hw1287789687:
怎么读取合并单元格中的内容呢?
使用JXL 读取Excel合并单元格方法 -
padangel:
狠不错,给我一些启发了
使用POI对Office Excel 97-2003 版本以及2007版进行基本的读取操作 -
yu165683:
thank you very much!
使用JXL 读取Excel合并单元格方法
1、index.jsp页面
<body>
<form action="excel_view.jsp" method="post">
<input type="file" name="filepath">
<input type="submit" name="submit" value="提交">
</form>
</body>
----------------------------------------------------------------------
2、excel_view.jsp页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="jxl.Cell"%>
<%@ page import="jxl.CellType"%>
<%@ page import="jxl.Sheet"%>
<%@ page import="jxl.Workbook"%>
<%@ page import="jxl.read.biff.BiffException"%>
<%@ page import="com.method.CellStyle" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>查看excel文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
try {
InputStream is = new FileInputStream(request.getParameter("filepath"));
Workbook read_workbook = Workbook.getWorkbook(is);
Sheet sheet = read_workbook.getSheet(0);
CellStyle cell_style = new CellStyle();
int rows = sheet.getRows();
int columns = sheet.getColumns();
int rowspan = 0;
int colspan = 0;
out.print("<table border='1' cellpadding='0' cellspacing='0' style='font-size: 12px;border-collapse:collapse' bordercolor='#000000'>");
for (int i = 0; i < rows; i++) {
out.print("<tr>");
for (int j = 0; j < columns ; j++) {
Cell cell = sheet.getCell(j, i);
cell_style.ReadRange(sheet,i,j);
rowspan = cell_style.getRowspan();
colspan = cell_style.getColspan();
String contents = null;
if(cell.getType() == CellType.EMPTY){
cell_style.setBetweenRowColumn(i,j);
int min_row = cell_style.getMin_row();
int max_row = cell_style.getMax_row();
int min_col = cell_style.getMin_column();
int max_col = cell_style.getMax_column();
if( min_row < i && i <= max_row && min_col <= j && j<= max_col){
j = max_col;
continue;
}
contents = " ";
out.print("<td width='"+(40*colspan)+"' rowspan='"+rowspan+"' colspan='"+colspan+"'>"+contents+"</td>");
j += colspan-1;
continue;
} else {
contents = cell.getContents();
out.print("<td align='center'"
+"rowspan='"+rowspan+"'"
+"colspan='"+colspan+"'"
+"bGcolor='"+cell_style.getBgcolor(cell)+"'"
//+" height='"+sheet.getSettings().getDefaultRowHeight()+"'"
+"height='25'"
+" width='"+(60*colspan)+"'>"
+"<Font color='"+cell_style.getFontColor(cell)+"'"
//+"size='"+cell.getCellFormat().getFont().getPointSize()+"'"
+"face='"+cell.getCellFormat().getFont().getName()+"'"
+">"+contents+"</Font></td>");
j += colspan-1;
continue;
}
}
out.print("</tr>");
}
read_workbook.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
%>
</body>
</html>
----------------------------------------------------------------------
3、CellStyle.java
package com.method;
import jxl.Cell;
import jxl.Range;
import jxl.Sheet;
/**
* 获取Excel 里被合并单元格的样式
* @author JinYunHe
*
*/
public class CellStyle {
private int topleft_row ; //起始行
private int topleft_column; //起始列
private int bottomright_row = 0; //终止行
private int bottomright_column = 0; //终止列
private int rowspan = 1; //行
private int colspan = 1; //列
private Range[] ranges = null; //合并单元格集合
private int min_row ; //最小行
private int max_row ; //最大行
private int min_column; //最小列
private int max_column; //最大列
/**
* 获取本sheet里合并单元格的集合
* @param sheet
*/
public void ReadRange(Sheet sheet,int row,int column){
ranges = sheet.getMergedCells();
for(Range space:ranges){
//System.out.print(space.getTopLeft().getRow()+"行,");
//System.out.print(space.getTopLeft().getColumn()+"列\t");
//System.out.print(space.getBottomRight().getRow()+"行,");
//System.out.print(space.getBottomRight().getColumn()+"列\n");
//System.out.println("\n-------------------------");
///*
this.topleft_row = space.getTopLeft().getRow();
this.topleft_column = space.getTopLeft().getColumn();
if(row != topleft_row || column != topleft_column){
this.setRowspan(1);
this.setColspan(1);
}
if(row == topleft_row && column == topleft_column){
this.setTopleft_row(this.topleft_row);
this.setTopleft_column(this.topleft_column);
this.setBottomright_row(space.getBottomRight().getRow());
this.setBottomright_column(space.getBottomRight().getColumn());
this.setRowspan(2);
this.setColspan(2);
break;
}
//*/
}
//System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
}
/**
*
* @return
*/
public void setBetweenRowColumn(int row ,int column){
for(Range space:ranges){
//获取行的范围
int min_row = space.getTopLeft().getRow(); // min row
int max_row = space.getBottomRight().getRow(); // max row
int min_column = space.getTopLeft().getColumn(); // min column
int max_column = space.getBottomRight().getColumn(); // max column
if(min_row <= row
&& row <= max_row){
//获取列的范围
if(min_column <= column
&& column <= max_column){
this.setMin_row(min_row); // min row
this.setMax_row(max_row); // max row
this.setMin_column(min_column); // min column
this.setMax_column(max_column); // max column
break;
}
}
}
}
/**
* 获取背景颜色的值
* @param cell: 当前单元格
* @return
*/
public String getBgcolor(Cell cell){
int r = cell.getCellFormat().getBackgroundColour().getDefaultRGB().getRed();
int g = cell.getCellFormat().getBackgroundColour().getDefaultRGB().getGreen();
int b = cell.getCellFormat().getBackgroundColour().getDefaultRGB().getBlue();
return "#"+Integer.toHexString(r<<16|g<<8|b);
}
/**
* 获取字体的颜色
* @param cell: 当前单元格
* @return
*/
public String getFontColor(Cell cell){
int r = cell.getCellFormat().getFont().getColour().getDefaultRGB().getRed();
int g = cell.getCellFormat().getFont().getColour().getDefaultRGB().getGreen();
int b = cell.getCellFormat().getFont().getColour().getDefaultRGB().getBlue();
return "#"+Integer.toHexString(r<<16|g<<8|b);
}
public int getTopleft_row() {
return topleft_row;
}
public void setTopleft_row(int topleft_row) {
this.topleft_row = topleft_row;
}
public int getTopleft_column() {
return topleft_column;
}
public void setTopleft_column(int topleft_column) {
this.topleft_column = topleft_column;
}
public int getBottomright_row() {
return bottomright_row;
}
public void setBottomright_row(int bottomright_row) {
this.bottomright_row = bottomright_row;
}
public int getBottomright_column() {
return bottomright_column;
}
public void setBottomright_column(int bottomright_column) {
this.bottomright_column = bottomright_column;
}
public int getRowspan() {
return rowspan;
}
public void setRowspan(int rowspan) {
// rowspan :1 表示无; 2 表示有;
if(rowspan == 1){
this.rowspan = 1;
}
if(rowspan == 2){
this.rowspan = this.getBottomright_row() - this.getTopleft_row() +1;
}
}
public int getColspan() {
return colspan;
}
public void setColspan(int colspan) {
// colspan :1 表示无; 2 表示有;
if(colspan == 1){
this.colspan = 1;
}
if(colspan == 2){
this.colspan = this.getBottomright_column() - this.getTopleft_column() +1;
}
}
//------------------------获取所合并的单元格的的所表示范围的行和列-----------
public Range[] getRanges() {
return ranges;
}
public void setRanges(Range[] ranges) {
this.ranges = ranges;
}
public int getMin_row() {
return min_row;
}
public void setMin_row(int min_row) {
this.min_row = min_row;
}
public int getMax_row() {
return max_row;
}
public void setMax_row(int max_row) {
this.max_row = max_row;
}
public int getMin_column() {
return min_column;
}
public void setMin_column(int min_column) {
this.min_column = min_column;
}
public int getMax_column() {
return max_column;
}
public void setMax_column(int max_column) {
this.max_column = max_column;
}
}
注: 这个例子虽能把excel里的数据输出到jsp页面上,但还不够完美: 还未实现读公式,布尔型等类型的值,同时字体大小不能很好的控制。字体颜色、单元格的背景色,都是通过计算得出的(*_o只因不会直接使用Colour 这个类。),同时代码也比较烦琐,希望有兴趣的高手,帮忙修改,先谢谢了!
发表评论
-
web应用生产环境properties文件自动更新
2016-12-06 23:04 582在我们的生产系统上,如果我们修改了properties中的 ... -
“手机号码输入控件“实现
2015-10-29 23:15 1019<! DOCTYPE html> < h ... -
fastjson的使用
2012-06-12 21:07 2072// 实体类Group定义 public class Gro ... -
JMAIL发送邮件时注意的问题
2012-05-14 17:01 1734最近使用jmail发送邮件,写了下面一段代码: String ... -
JavaMail邮件发送异常:{exception=java.lang.SecurityException: Access to default session
2012-02-09 09:52 3142起因:最近使用JavaMail发送邮件,WEB容器我用的是re ... -
使用POI对Office Excel 97-2003 版本以及2007版进行基本的读取操作
2011-03-18 15:11 2358使用POI对excel不同的版本进行读操作,参见以下代码: ... -
Java开发的WebService获取客户端IP地址
2010-11-17 10:46 71981.基于jax-ws开发的webservice获取客户端ip的 ... -
Java使用Axis调用.NET语言开发的WebService
2010-11-17 10:42 1699Java结合Axis调用.net语言开发的WebService ... -
DES加密与解密
2010-03-17 18:29 1137===================使用DES进行加密和解密 ... -
Sax 解析xml文件时, 数据信息不完整问题解决方法
2010-01-04 13:31 3793使用Sax对xml文件进行解析时,Sax每次只读入2k数据,当 ... -
Struts 中常见的错误
2009-03-23 11:36 9151、“No bean found under att ... -
使用JXL 读取Excel合并单元格方法
2009-03-07 11:36 9293import java.io.FileInputStrea ... -
org.apache.struts.taglib.html.BEAN
2009-03-05 12:40 42821、Cannot find bean under name o ...
相关推荐
在jsp中用Java读取Excel中的数据,以便把该数据写入数据库或jsp页面,对Excel导入很有帮助
jsp+jspsmart上传+poi3.8读取excel2007+jxl读取excel2003
JXL(Java操作Excel文件Oracle数据库)
jxl用于读取Excel的Java类(个人总结综合教程,适合新入门的朋友阅读)
这个代码详细,只需要修改与你代码读取文件相同就OK了,基本不需要修改什么,有任何问题请M我!
Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为是使用Java编写的...
Java Excel 是一开放源码项目,通过它Java 开发人员可以读取Excel 文件的内容、创建新的Excel 文件、更新已经存在的Excel 文件。使用该API 非Windows操作系统也可以通过纯Java 应用来处理Excel 数据表。因为是使用...
Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为是使用Java编写的...
Java实现从excel中批量高效导入数据到数据库中,包括图片的导入存储,读取图片显示在jsp页面等,需要下载JspSmartUpload.jar和jxl.jar两个jar包,将两个包导入项目才能运行
JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为它是使用...