EXCLEのプレビューページ

もともと、Aspose社-細胞は、PDFに変身し、オンラインプレビューを実装したいXLSXしようとしたが、スイッチが与えられていないときに、が、長い時間のために、オンラインのターンをチェックを見ていないことが判明したとき

、本物をピックアップしていることを意味し、同様のケースを見つけるの拡張子を変更する唯一の私が共著者は、ああ与えると判断透かしを追加することで、最初の発見後に変更と同じくらい透かしであることを確認することができます

 

私は後でそれを変更する変更するには、プレビューすることができ、HTMLに変身して、インターネットのようなツールを見つけるに入れたいです

私はそれがシートのプレビューを表示することですすべて一緒に何かを追加しました

 

最初は、JARパッケージです

 

<依存性>
<のgroupId> org.apache.poi </のgroupId>
<たartifactId> POI </たartifactId>
<バージョン> 3.9 </バージョン>
</依存>
<依存性>
<のgroupId> org.apache.poi </のgroupId>
<たartifactId> POI-OOXML </たartifactId>
<バージョン> 3.9 </バージョン>
</依存>
<依存性>
<のgroupId> org.apache.poi </のgroupId>
<たartifactId> POI-OOXML-スキーマ</たartifactId>
<バージョン> 3.9 </バージョン>
</依存>
<依存性>
<のgroupId> org.apache.poi </のgroupId>
<たartifactId> POI-スクラッチパッド</たartifactId>
<バージョン> 3.9 </バージョン>
</依存関係>

 

次いで、受信コード

 

<ボタンタイプ= "ボタン" ID = "excletopdf">文件预览</ボタン>エクセル

jqueryの:

$( "#1 excletopdf")に( "クリック"、関数(){
window.open( "/ B2B /エクセル/プレビュー");
})

 

背景コード

-------------------------------------------------- -------------------------------------------------- ----------------------------------------------

コントローラ:

 

@Controller
@RequestMapping( "/エクセル")
パブリッククラスExcelPreview {

// Javaのウェブエクセル在网页预览
@RequestMapping( "/プレビュー")
公共ボイドSS(HttpServletResponseの応答)例外{投げる
。新しいExcelToHTML()printreadExcelToHtml(応答を、 "D:\\テスト\\ 11.xlsx"、真) ;
}
}

-------------------------------------------------- -------------------------------------------------- ----------------------------------------------

サービス:

 

 

輸入org.apache.poi.hssf.usermodel *。;
輸入org.apache.poi.hssf.util.HSSFColor。
輸入org.apache.poi.ss.usermodel *。;
輸入org.apache.poi.ss.util.CellRangeAddress。
輸入org.apache.poi.xssf.usermodel.XSSFCellStyle。
輸入org.apache.poi.xssf.usermodel.XSSFColor。
輸入org.apache.poi.xssf.usermodel.XSSFFont;
輸入org.apache.poi.xssf.usermodel.XSSFWorkbook;
輸入org.junit.Test;

輸入com.aspose.words.Document。
輸入com.aspose.words.License。
輸入com.aspose.words.SaveFormat。

輸入java.io. *;
輸入java.text.DecimalFormatの。
輸入java.text.SimpleDateFormatの。
輸入java.util.Date;
輸入java.util.HashMapを;
輸入java.util.Map;

インポートのjavax.servlet.http.HttpServletResponse;

パブリッククラスExcelToHTML {

 

//私は自分でラインに、元の要件をコメントアウト

//シートのhtmlの詳細を達成することができ、このメソッドを追加

getExcelInfo静的な文字列(ブックWB、ブールisWithStyle)プライベート{


// wb.getSheetAtシートシート=(0); //最初のシートの内容を取得

// workbook.getNumberOfSheetsを();の印刷枚数
のSystem.out.println (「----------------」+ wb.getNumberOfSheets());
あなたが印刷されたシートのすべてをしたい場合は、それになるために戻っている//

STR =「」文字列;
のために( 0 =私はint型、Iはwb.getNumberOfSheetsを<(); I)が{++

STR + = getExcelInfo(WB、wb.getSheetAt(I)、isWithStyle);


STR + =「<BR /> <BR / >「; // 4行の間隔でシート

}





戻りSTR;
}




プライベート静的な文字列getExcelInfo(WBワークブック、シート・シート、ブールisWithStyle){
StringBufferのStringBufferの新しい新しいSB =();

int型lastRowNum sheet.getLastRowNum =();
地図<文字列、文字列>地図は、[] = getRowSpanColSpanMap(シート)。
sb.append( "<テーブルのスタイル= '国境崩壊:崩壊;'幅= '100%'>");
行の行= NULL; //兼容
電池セル= NULL; //兼容

ため(INTたrowNum = sheet.getFirstRowNum();たrowNum <= lastRowNum;たrowNum ++){
列= sheet.getRow(たrowNum)。
IF(行== NULL){
sb.append( "<TR> <TD> <NOBR> </ NOBR> </ TD> </ TR>");
持続する;
}
sb.append( "<TR>");
INT lastColNum = row.getLastCellNum()。
用(INT colNum引数= 0; colNum引数<lastColNum; colNum引数++){
細胞= row.getCell(colNum引数)。
IF(セル== NULL){//特殊情况空白的单元格会返回ヌル
sb.append( "<TD> </ TD>");
持続する;
}

文字列のstringValue = getCellValue(セル)
IF(マップ[0] .containsKey(たrowNum + "" + colNum引数)){
ストリングpointString =マップ[0]に.get(たrowNum + "" + colNum引数)。
地図[0] .remove(たrowNum + "" + colNum引数)。
INT bottomeRow = Integer.valueOf(pointString.split( "")[0])。
INT bottomeCol = Integer.valueOf(pointString.split( "")[1])。
int型ROWSPAN = bottomeRow -たrowNum + 1;
int型COLSPAN = bottomeCol - colNum引数+ 1;
sb.append( "<TD ROWSPAN =「" + ROWSPAN + " 'COLSPAN ='" + COLSPAN +「」「)。
}そうであれば(マップ[1] .containsKey(たrowNum + "" + colNum引数)){
地図[1] .remove(たrowNum + "" + colNum引数)。
持続する;
}他{
sb.append( "<TD")。
}

//判断是否需要样式
IF(isWithStyle){
dealExcelStyle(WB、シート、セル、



IF(のstringValue == NULL || "" .equals(stringValue.trim())){
sb.append(」「)。
}他{
//将ASCII码为160的空格转换为HTML下的空格()
sb.append(stringValue.replace(String.valueOf((CHAR)160)、」「));
}
sb.append( "</ NOBR> </ TD>");
}
sb.append( "</ TR>");
}

sb.append( "</ TABLE>");

sb.toStringを返します();
}







プライベート静的地図<文字列、文字列> [] getRowSpanColSpanMap(シートシート){

地図<文字列、文字列> MAP0 =新規HashMapの<文字列、文字列>();
地図<文字列、文字列> MAP1 =新しいHashMapの<文字列、文字列>();
INT mergedNum = sheet.getNumMergedRegions()。
CellRangeAddress範囲= NULL;
以下のために(INT i = 0; iは<mergedNum; iは++){
範囲= sheet.getMergedRegion(I)。
INT topRow = range.getFirstRow()。
INT topCol = range.getFirstColumn()。
INT bottomRow = range.getLastRow()。
INT bottomCol = range.getLastColumn()。
map0.put(topRow + "" + topCol、bottomRow + "" + bottomCol)。
//のSystem.out.println(topRow + "" + topCol + "" + bottomRow + "" + bottomCol)。
int型tempRow = topRow。
一方、(tempRow <= bottomRow){
int型tempCol = topCol。
一方、(tempCol <= bottomCol){
map1.put(tempRow + "" + tempCol、 "");
tempCol ++;
}
tempRow ++。
}
map1.remove(topRow + "" + topCol)。
}
、[] = {地図MAP0地図 MAP1}。
マップを返します。
}


/ **
*セルの捕捉テーブルセルの内容
* @paramセル
* @return
* /
プライベート静的な文字列getCellValue(セルCELL){

文字列の結果=新しい新しいString();
スイッチ(cell.getCellType()){
ケースCell.CELL_TYPE_NUMERIC: //番号型
IF(HSSFDateUtil.isCellDateFormatted(セル)){ // トランザクションの日付または時刻の形式
のSimpleDateFormat SDF = NULL;
IF(cell.getCellStyle()getDataFormat()== HSSFDataFormat.getBuiltinFormat( "H:MM")。 ){
SDFてSimpleDateFormat新しい新=( "HH:MM");
} //他{日
SDFてSimpleDateFormat新しい新=( "MM-DD-YYYY");
}
日付cell.getDateCellValue =日付();
結果= sdf.format( DATE);
}そうIF(cell.getCellStyle()getDataFormat()== 58){
//カスタム処理の日付形式は:m MMMのD(IDフォーマットはセルを決定することによって解決され、IDの値は58である)
のSimpleDateFormatてSimpleDateFormat SDF新しい新=( "MM-DD-YYYY");
ダブルcell.getNumericCellValue値=( );
日付= org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(値);
結果= sdf.format(DATE);
} {他
ダブルcell.getNumericCellValue値=();
CellStyleスタイル= cell.getCellStyle( );
てDecimalFormat新しい新しい=てDecimalFormat形式();
文字列style.getDataFormatStringのTEMP =();
//従来のセル設定
(temp.equals( "一般的な" IF)){
format.applyPattern( "#");
}
結果を= format.format(値);
}
BREAK;
ケースCell.CELL_TYPE_STRING://文字列型
= cell.getRichStringCellValue結果()のtoString();.
BREAK;
ケースCell.CELL_TYPE_BLANK:
  結果= "&NBSPは、";もともと// = ""その結果、これは非常に小さな単位を行いますが、細胞を保持することができるようになりますA&NBSPを追加しました良く見
BREAK;
デフォルト:
  結果= "&NBSPは";
BREAK;
}
結果を返す;
}

/ **
*表スタイルでの作業
* @paramのWB
* @paramシート
* @param SB
* /
プライベート静的ボイドdealExcelStyle(ワークブックWB、シートシート、セルCELL、StringBufferのSB){

CellStyle cell.getCellStyle CellStyle =();
IF(CellStyle = NULL){!
ショートcellStyle.getAlignmentアライメント=();
// sb.append( "ALIGN =左=「" + convertAlignToHtml(アライメント)+ ""「); //セル内容水平配向
短いVerticalAlignmentを= cellStyle.getVerticalAlignment()。
sb.append( "VALIGN ="" + convertVerticalAlignToHtml(VerticalAlignmentを)+「」「); //单元格中内容的垂直排列方式

IF(XSSFWorkbook instanceofのWB){

XSSFFont XF =((XSSFCellStyle)cellStyle).getFont() ;
短いboldWeight = xf.getBoldweight()。
文字列ALIGN = convertAlignToHtml(アライメント)
sb.append( "スタイル="");
sb.append( "フォント重量:" + boldWeight + ";"); //字体加粗
sb.append( "フォントサイズ:" + xf.getFontHeight()/ 2 + "%;"); //字体大小
int型columnWidthの= sheet.getColumnWidth(cell.getColumnIndex());
sb.append( "幅:" + columnWidthの+ "PX;");
sb.append(「テキスト整列:」+「+揃えます。

もし"(XC = NULL &&"!{(XC)と等しい。)
色:# "+ xc.getARGBHex()サブストリング(2)+。 ";"); sb.append"を //字体颜色
}

XSSFColorはbgColor =(XSSFColor)cellStyle.getFillForegroundColorColor();
IF(はbgColor = NULL && "!"に等しい(はbgColor)){
sb.append( "背景色:#" + bgColor.getARGBHex()サブストリング(2)+ ";"); //背景颜色
}
sb.append(getBorderStyle(0、cellStyle.getBorderTop()、((XSSFCellStyle)cellStyle).getTopBorderXSSFColor()))。
sb.append(getBorderStyle(1、cellStyle.getBorderRight()、((XSSFCellStyle)cellStyle).getRightBorderXSSFColor()))。
sb.append(getBorderStyle(2、cellStyle.getBorderBottom()、((XSSFCellStyle)cellStyle).getBottomBorderXSSFColor()))。
sb.append(getBorderStyle(3、cellStyle.getBorderLeft()、((XSSFCellStyle)CellStyle).getLeftBorderXSSFColor()));

}そうIF(WB HSSFWorkbookのinstanceof){

HSSFFont HF =((HSSFCellStyle)CellStyle).getFont(WB) ;
ショートboldWeight hf.getBoldweight =();
ショートのfontColor hf.getColor =();
sb.append( "スタイル=「");
HSSFPaletteパレット=((HSSFWorkbook)WB).getCustomPalette(); //クラスのHSSFPalette国際標準形式必要な色
HSSFColor HC = palette.getColor(のfontColor);
sb.append( "フォント重量:" + boldWeight + ";"); //太字
sb.append( "フォントサイズ:" + hf.getFontHeight()/ 2 + " %;"); // フォントサイズの
文字列ALIGN =左= convertAlignToHtml(アライメント);
sb.append( "テキストALIGN =左:" ALIGN =左+ + ";"); //ヘッダ組版スタイル
ストリングfontColorStr = convertToStardColor(HC)。
(!! "と等しい(fontColorStr.trim()){fontColorStr = NULL &&")もし
sb.append( "色:"; "+ fontColorStr +")。//字体颜色
}
INT columnWidthの= sheet.getColumnWidth(cell.getColumnIndex())。
sb.append( "幅:" + columnWidthの+ "PX;");
短いはbgColor = cellStyle.getFillForegroundColor()。
HC = palette.getColor(はbgColor)。
ストリングbgColorStr = convertToStardColor(HC)。
IF(!! "と等しい(bgColorStr.trim()){bgColorStr = NULL &&")
sb.append( "背景色:" + bgColorStr + ";"); //背景颜色
}
sb.append(getBorderStyle(パレット、0、cellStyle.getBorderTop()、cellStyle.getTopBorderColor()))。
sb.append(getBorderStyle(パレット、1、cellStyle.getBorderRight()、cellStyle.getRightBorderColor()))。
sb.append(getBorderStyle(パレット、3、cellStyle.getBorderLeft()、cellStyle.getLeftBorderColor()))。
sb.append(getBorderStyle(パレット、2、cellStyle.getBorderBottom()、cellStyle.getBottomBorderColor()))。
}

sb.append(「」「)。
}
}

/ **
*单元格内容的水平对齐方式
* @paramアライメント
* @return
* /
プライベート静的な文字列convertAlignToHtml(短いアライメント){

文字列ALIGN = "センター"。
スイッチ(アライメント){
ケースCellStyle.ALIGN_LEFT:
= "左" ALIGN。
ブレーク;
ケースCellStyle.ALIGN_CENTER:
=「センター」を整列させます。
ブレーク;
ケースCellStyle.ALIGN_RIGHT:
=「右」を揃えます。
ブレーク;
デフォルト:
休憩;
}
ALIGNを返します。
}

/ **
*单元格中内容的垂直排列方式
* @param VerticalAlignmentを
* @return
* /
プライベート静的な文字列convertVerticalAlignToHtml(ショートVerticalAlignmentを){

文字列VALIGN = "中";
スイッチ(VerticalAlignmentを){
ケースCellStyle.VERTICAL_BOTTOM:
VALIGN = "底";
ブレーク;
ケースCellStyle.VERTICAL_CENTER:
VALIGN = "センター"。
ブレーク;
ケースCellStyle.VERTICAL_TOP:
VALIGN = "トップ"。
ブレーク;
デフォルト:
休憩;


}

プライベート静的な文字列convertToStardColor(HSSFColorのHC){

StringBufferのSB =新規のStringBuffer( "")。
IF(!HC = NULL){
IF(HSSFColor.AUTOMATIC.index == hc.getIndex()){
戻りヌル。
}
sb.append( "#")。
以下のために(。; I <hc.getTriplet()長さ; I = 0をint型私は++){
sb.append(fillWithZero(Integer.toHexString(hc.getTriplet()[I])))。
}
}

)(sb.toStringを返します。
}

プライベート静的な文字列fillWithZero(文字列str){
IF(STR = NULL && str.length()<2!){
リターン"0" + STR。
}
STRを返します。
}

静的文字列[] bordesr = { "ボーダートップ"、 "ボーダー右:"、 "ボーダー底:"、」
静的文字列[] borderStyles = { "固体"、 "固体"、 "固体"、 "固体"、 "固体"、 "固体"、 "固体"、 "固体"、 "固体"、 "固体"、 "固体" }、「固体」、「固体」、「固体」。

プライベート静的な文字列getBorderStyle(HSSFPaletteパレット、int型B、短いS、短いT){

IF(S == 0)戻りbordesr [B] + borderStyles [S] + "#1 d0d7e5の1ピクセル;" ;;
ストリングborderColorStr = convertToStardColor(palette.getColor(T))。
borderColorStr = borderColorStr == nullを|| borderColorStr.length()<1 "#000000":borderColorStr;?
bordesr戻り[B] + borderStyles [S] + borderColorStr + "1ピクセル;";

}

プライベート静的な文字列getBorderStyle(int型B、短いS、

XSSFColorのXC){ IF(S == 0)戻りbordesr [B] + borderStyles [S] + "#1 d0d7e5の1ピクセル;" ;;
(XCは!= nullの&&! ""。(XC)に等しい){場合
文字列borderColorStr = xc.getARGBHex(); // t.getARGBHex();
borderColorStr = borderColorStr == nullを|| borderColorStr.length()<1 "#000000":?borderColorStr.substring(2);
bordesr戻り[B] + borderStyles [S] + borderColorStr + "1ピクセル;";
}

リターン「」。
}





//以下是我自己写的借鉴第一个方法
パブリック静的ボイドprintreadExcelToHtml(HttpServletResponseの応答文字列filePathに、ブールisWithStyle){

入力ストリームは= nullです。
文字列htmlExcel = NULL;
試す{
ファイル自体のソース=新しいファイル(filePathに);
ある=新しいFileInputStreamを(ソースファイル名);
ワークブックWB = WorkbookFactory.create(です)。
IF(XSSFWorkbook instanceofのWB){// 03版エクセル处理方法
XSSFWorkbook XWB =(XSSFWorkbook)WB。
htmlExcel = ExcelToHTML.getExcelInfo(XWB、isWithStyle)。
}他(HSSFWorkbook instanceofのWB){// 07及10版以后的エクセル处理方法もし
HSSFWorkbook HWB =(HSSFWorkbook)WB。
htmlExcel = ExcelToHTML.getExcelInfo(HWB、isWithStyle)。
}
priteFile(応答、htmlExcel)。
}キャッチ(例外e){
e.printStackTrace();
}最後に{
{試みる
is.closeを();
}キャッチ(IOExceptionを電子){
e.printStackTrace();
}
}

}



//输出流打印出来
プライベート静的ボイドpriteFile(HttpServletResponseの応答は、文字列のコンテンツ)が例外{スロー

StringBuilderのSB =新規のStringBuilder();


sb.append( "<HTML> <HEAD> <META HTTP-当量= \" Content-Typeの\」コンテンツ= \ "text / htmlの;のcharset = UTF-8 \"> <タイトル> Htmlのテスト</ TITLE> < / HEAD> <BODY> ");
sb.append( "の<div>");
sb.append(コンテンツ)。
sb.append( "</ DIV>");
sb.append( "</ body> </ html>この");


FileInputStreamのビス= NULL;
OutputStreamのOS1 = NULL;
FileOutputStream OS = NULL;
試す{

ファイルfile1 =新しいファイル( "D:\\テスト");
(!file1.exists()){//目录不存在就创建場合
file1.mkdirs();
}
ファイルファイル=新しいファイル( "D:\\試験\\ 11.html")。//新建一个空白PDF文档
IF(file.exists()){
file.delete()。






printStream.println(sb.toString()); //将字符串写入文件

//输出到浏览器
response.setContentType( "text / htmlの;のcharset = UTF-8");
//response.setContentType("application/pdf ");
ビス=新規のFileInputStream(file.getPath())。
OS1 = response.getOutputStream()。
int型のカウント= 0;
バイト[]バッファ=新しいバイト[* 1024 1024];
(!(カウント= bis.read(バッファ))= -1){一方
os1.write(バッファ、0、カウント)。
}
os1.flush()。
}キャッチ(例外e){
e.printStackTrace();
}最後に{
IF(!OS1 = NULL){
os1.close()。
}
IF(!OS = NULL){
にos.close()。
}
IF(!ビス= NULL){
bis.close()。
}
}


}

}

 

 

これらは、〜を利用することができるの変化に変更することで構築された私自身のテストプログラムです

 

おすすめ

転載: www.cnblogs.com/Mr-Y1907/p/11512387.html