【Code】Excel轉成Html

網上找到的

package excel2html;

/**
* excel信息表
* @author Administrator
*
*/
public class VExcelInf {
private int row ;//所在跨行
private int clum;//所在列
private String front;//字體
private int backcolorR;//背景色r
private int backcolorG;//背景色g
private int backcolorB;//背景色b
private int fontcolorR;//字體顏色r
private int fontcolorG;//字體顏色g
private int fontcolorB;//字體顏色b
private int khang;//跨行
private int klie;//跨列
private int height;//高度
private int width;//寬度
private String content;//內容
private boolean useable;//該單元格是否可用
private String alignment;//對其方式
private String cellType;//單元格類型:日期,數字,布爾,標籤
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getClum() {
return clum;
}
public void setClum(int clum) {
this.clum = clum;
}
public String getFront() {
return front;
}
public void setFront(String front) {
this.front = front;
}
public int getBackcolorR() {
return backcolorR;
}
public void setBackcolorR(int backcolorR) {
this.backcolorR = backcolorR;
}
public int getBackcolorG() {
return backcolorG;
}
public void setBackcolorG(int backcolorG) {
this.backcolorG = backcolorG;
}
public int getBackcolorB() {
return backcolorB;
}
public void setBackcolorB(int backcolorB) {
this.backcolorB = backcolorB;
}
public int getFontcolorR() {
return fontcolorR;
}
public void setFontcolorR(int fontcolorR) {
this.fontcolorR = fontcolorR;
}
public int getFontcolorG() {
return fontcolorG;
}
public void setFontcolorG(int fontcolorG) {
this.fontcolorG = fontcolorG;
}
public int getFontcolorB() {
return fontcolorB;
}
public void setFontcolorB(int fontcolorB) {
this.fontcolorB = fontcolorB;
}
public int getKhang() {
return khang;
}
public void setKhang(int khang) {
this.khang = khang;
}
public int getKlie() {
return klie;
}
public void setKlie(int klie) {
this.klie = klie;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isUseable() {
return useable;
}
public void setUseable(boolean useable) {
this.useable = useable;
}
public String getAlignment() {
return alignment;
}
public void setAlignment(String alignment) {
this.alignment = alignment;
}
public String getCellType() {
return cellType;
}
public void setCellType(String cellType) {
this.cellType = cellType;
}


}


package excel2html;

import java.util.ArrayList;
import java.util.List;

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

public class ReadExcel {

public static List<VExcelInf>[] importExcelInf(Workbook wb){

if(wb==null){
throw new RuntimeException("表格信息爲空");
}
Sheet st = wb.getSheet(0);
if(st.getRows()>25000){
throw new RuntimeException("讀取信息過大,不能讀取:最大行數 "+25000);
}
Cell[] myCells;
List<VExcelInf>[]listArray = new ArrayList[st.getRows()];
for (int i = 0; i < st.getRows(); i++) {
try {
listArray[i] = new ArrayList<VExcelInf>();
myCells = st.getRow(i);
for (int j = 0; j < myCells.length; j++) {
if(myCells[j].getContents()==null){
return null;
}
VExcelInf temp =new VExcelInf();
if(myCells[j]!=null){
temp.setContent(myCells[j].getContents());//內容
temp.setCellType(myCells[j].getType().toString()=="empty"?null:myCells[j].getType().toString());
temp.setClum(myCells[j].getColumn());//所在列
temp.setRow(myCells[j].getRow());//所在行
}
if(myCells[j].getCellFormat()!=null){
temp.setBackcolorB(myCells[j].getCellFormat().getBackgroundColour().getDefaultRGB().getBlue());
temp.setBackcolorG(myCells[j].getCellFormat().getBackgroundColour().getDefaultRGB().getGreen());
temp.setBackcolorR(myCells[j].getCellFormat().getBackgroundColour().getDefaultRGB().getRed());
temp.setFontcolorB(myCells[j].getCellFormat().getFont().getColour().getDefaultRGB().getBlue());
temp.setFontcolorG(myCells[j].getCellFormat().getFont().getColour().getDefaultRGB().getGreen());
temp.setFontcolorR(myCells[j].getCellFormat().getFont().getColour().getDefaultRGB().getRed());
temp.setFront(myCells[j].getCellFormat().getFont().getScriptStyle().getDescription());// 字體
// 部分信息未讀取成功
}
else{
temp.setBackcolorB(255);
temp.setBackcolorG(255);
temp.setBackcolorR(255);
}
listArray[i].add(temp);
}

} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("讀取excel表格信息失敗");
}
}
//讀取跨行跨列
// Range[] ranges = st.getMergedCells();
// if(ranges!=null)
// for (int i = 0; i < ranges.length; i++) {
// Range range = ranges[i];
// if(range.getBottomRight().getColumn()!=range.getTopLeft().getColumn()&&range.getBottomRight().getRow()!=range.getTopLeft().getRow()){
// listArray[range.getTopLeft().getRow()].get(range.getTopLeft().getColumn()).setKhang((range.getBottomRight().getRow()-range.getTopLeft().getRow()));
// listArray[range.getTopLeft().getRow()].get(range.getTopLeft().getColumn()).setKlie((range.getBottomRight().getColumn()-range.getTopLeft().getColumn()));
//
// }
// else if(range.getBottomRight().getColumn()==range.getTopLeft().getColumn()){
// listArray[range.getTopLeft().getRow()].get(range.getTopLeft().getColumn()).setKhang((range.getBottomRight().getRow()-range.getTopLeft().getRow()));
// }
// else if(range.getBottomRight().getRow()==range.getTopLeft().getRow()){
// listArray[range.getTopLeft().getRow()].get(range.getTopLeft().getColumn()).setKlie((range.getBottomRight().getColumn()-range.getTopLeft().getColumn()));
// }
//
// }
//跨行跨列後,標記他所覆蓋的單元格
// for (int i = 0; i < listArray.length; i++) {
// for (int j = 0; j < listArray[i].size(); j++) {
// boolean b=false ;
// //如果是跨行、跨列則被跨的行和列不顯示,比如跨2行、2列,則該列後的2列設置爲不顯示,該行後的兩行設置爲不顯示
// for (int j2 = i; j2 <i+listArray[i].get(j).getKhang()+1; j2++) {
// for (int k = j; k < j+listArray[i].get(j).getKlie()+1; k++) {
// if(b)
// listArray[j2].get(k).setUseable(true);
// b= true;
// }
// }
// }
// }
// for (int i = 0; i < listArray.length; i++) {
// for (int j = 0; j < listArray[i].size(); j++) {
// if(!listArray.get(j).isUseable()){
// if(listArray.get(j).getKhang()!=0)
// listArray.get(j).setKhang(listArray.get(j).getKhang()+1);
// if(listArray.get(j).getKlie()!=0)
// listArray.get(j).setKlie(listArray.get(j).getKlie()+1);
// }
// }
// }
return listArray;
}
}


package excel2html;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
* excel錶轉html表方法
* 本方法基於jxl,對圖片、公式支持較差。
* 能將表格的內容、跨行、跨列、背景色轉換成html樣式,表格的高度寬度屬於常量,並未從表格中讀取
* 轉換後將丟失的信息:高度、寬度、字體顏色、字體、圖片、公式、
* @author lxs
*
*/
public class Excel2Html {
public Excel2Html(){
this.setFontSize(15);
}
private int fontSize;//字體大小
public int getFontSize() {
return fontSize;
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
/**
* 根據輸入流組裝html,並寫入到輸出流
* @param insExcel
* @param osHtml
* @throws IOException
* @throws BiffException
*/
public void change2Html(InputStream insExcel,OutputStream osHtml) throws IOException,BiffException{
Workbook wb = null;
try {
wb = Workbook.getWorkbook(insExcel);
} catch (Exception e) {
throw new RuntimeException("文件格式不正確,必須傳入excel文件");
}
List<VExcelInf>[]listArray ;
listArray =ReadExcel.importExcelInf(wb);
// String tem=" height:auto; width:1000px;max-width:1000px; text-align:center; ";
String html ="<html xmlns:t="+"http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"+">";
String style ="<style> <!--table .xl66 {mso-style-parent:style0; border:.2pt solid windowtext;} --> </style>";
String body= "<body>";
String table ="<table border=0 cellspacing=0 style=font-size:"+this.getFontSize()+" cellpadding=0 class=ddTab id=mainTab>";
List<StringBuffer> tableString= new ArrayList<StringBuffer>();
tableString.add(new StringBuffer(html));
tableString.add(new StringBuffer(style));
tableString.add(new StringBuffer(body));
tableString.add(new StringBuffer(table));
for (int i = 0; i < listArray.length; i++) {
tableString.add(new StringBuffer( "<tr>"));
for (int j = 0; j < listArray[i].size(); j++) {
if(!listArray[i].get(j).isUseable()){
StringBuffer td =null;
StringBuffer tempBoder=null;
StringBuffer rgbAndHeight=new StringBuffer("");
if(!(listArray[i].get(j).getBackcolorR()==255&&listArray[i].get(j).getBackcolorG()==255&&listArray[i].get(j).getBackcolorB()==255))
rgbAndHeight =new StringBuffer(" bgcolor=rgb("+listArray[i].get(j).getBackcolorR()+","+listArray[i].get(j).getBackcolorG()+","+listArray[i].get(j).getBackcolorB()+")");
if(i==0&&j==0)
;
else if(j==0){
tempBoder =new StringBuffer("style='border-top:none'");
}
else if(i==0){
tempBoder=new StringBuffer("style='border-left:none'");
}
else
tempBoder=new StringBuffer("style='border-left:none ; border-top:none'");
td =new StringBuffer(" <td class=xl66 "+tempBoder+rgbAndHeight+" colspan="+listArray[i].get(j).getKlie()+" rowspan="+listArray[i].get(j).getKhang()+">"+listArray[i].get(j).getContent()+"  </td>");
tableString.add(td);
}
}
tableString.add(new StringBuffer("</tr>"));
if(i%1000==0){
bufferWrite(tableString, osHtml);
tableString.clear();
continue;
}
}
for (int i = 0; i <tableString.size(); i++) {
if(tableString.get(i)!=null&&tableString.get(i).toString()!=null)
osHtml.write((tableString.get(i).toString()+"\r\n").getBytes());
}
osHtml.write((new StringBuffer( "</table>")+"\r\n").getBytes());
osHtml.write((new StringBuffer( "</body>")+"\r\n").getBytes());
osHtml.close();
tableString=null;
listArray=null;
}
/**
* 根據輸入流把流文件轉換成實體html文件
* @param excelIn
* @param htmlFileName
* @throws IOException
* @throws BiffException
*/
public void change2Html(InputStream excelIn,String htmlFileName) throws IOException,BiffException{
OutputStream outpsForHtml = new FileOutputStream(htmlFileName);
this.change2Html(excelIn, outpsForHtml );
}

/**
* 根據倆個excel實體文件和excel名字 地址把html轉換成excel實體
* @param excelFileName
* @param htmlFileName
* @throws IOException
* @throws BiffException
*/
public void change2Html(String excelFileName,String htmlFileName) throws IOException,BiffException{
FileInputStream in = new FileInputStream(excelFileName);
change2Html(in, htmlFileName);
}
/**
* 將字符串緩衝寫入輸出流以便減小內存開銷
* @param tableString
* @param osHtml
* @throws IOException
*/
private void bufferWrite(List<StringBuffer> tableString ,OutputStream osHtml) throws IOException{
for (int i = 0; i <tableString.size(); i++) {
if(tableString.get(i)!=null&&tableString.get(i).toString()!=null)
osHtml.write((tableString.get(i).toString()+"\r\n").getBytes());
}
}

public static void main(String[] args) throws BiffException, IOException{

Excel2Html e2h = new Excel2Html();
e2h.setFontSize(12);
e2h.change2Html("E:\\a.xls","E:\\a.htm");

}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章