Andriod中的文档在线查看功能,类似于网易邮箱大师中的附件预览功能,要求在app内直接打开office文档、pdf文档等。
思路一:后台统一转换文档格式,安卓端只预览一种格式文档。
在后台将office(Word、Excle、PPT)文档转换为pdf文档,在安卓端预览的时候,其实是从后台先把pdf文档下载到手机上,再去读取。
-
后台:openoffice + jodconverter将office文档转换为pdf格式。
集成难度不大,网上教程非常多,需要在服务器上安装openoffice,转换速度很慢(页数多特别明显,页数少的话速度在可以接受的范围之内,转换速度与文档大小关系不大,主要是文档页数)。
-
安卓端使用AndroidPdfViewer第三方库显示pdf文档。
就是使用第三方库,也很简单。
思路二:在安卓端完成文档转换
- 使用Apache的Poi组件,但是由于ppt在转换过程中需要用到java的awt,所以无法实现ppt的转换,也就是说只能实现word、excle的转换。如果项目中只是有转换word或者excle的需求,可以采用Poi。
- 具体是:使用Poi将doc、docx、xls、xlsx文档转换为html,再使用webview加载本地html。
- 这里要注意的是,Poi的jar包的导入。如果去apache的poi官网查看各组件的功能,以及要导入的jar包,但是网上的代码一般都是org.apache.poi.xwpf.converter.xhtml
这个库的。等你导入了这个库,又自己从官网上下载了poi、poi-ooxml等jar包引入后,很可能会导致编译出错,提示有重复文件。这是由于导入的jar包的版本不一致导致的。正确的导入方式可以参考下面的:
这个会自动把关联的jar包引入,从maven仓库可以看到这个jar的依赖,以及依赖的依赖,
总之很多,可以在project视图中External Libraries来查看这个库文件到底包含了哪些jar包。
compile 'fr.opensagres.xdocreport:org.apache.poi.xwpf.converter.xhtml:1.0.6'
---------------------------------------------------------------------------------------------------
这个主要是doc的转换用到的,可以不引入,网上也有很多转换方法没有用到这个库
//compile 'fr.opensagres.xdocreport:fr.opensagres.xdocreport.document:1.0.6'
---------------------------------------------------------------------------------------------------
这个库文件的版本至关重要,因为...xhtml:1.0.6中依赖的poi等jar的版本就是3.10-FINAL,
所以这里必须也是3.10-FINAL,不然编译肯定不会通过。
compile 'org.apache.poi:poi-scratchpad:3.10-FINAL'
总之,如果你在apache.poi官网上看各个组件之间的依赖关系有些困难,可以去maven看。
- 除了引入库文件以外,还需要在app的gradle文件中添加如下语句:
defaultConfig {
...
multiDexEnabled true
...
}
packagingOptions {
...
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
dexOptions {
javaMaxHeapSize "3g"
jumboMode = true
preDexLibraries = false
}
lintOptions {
abortOnError false
}
project.tasks.withType(com.android.build.gradle.tasks.Dex) {
additionalParameters = ['--core-library']
}
思路三:其他
POI由于预览效果不是很好,不建议使用(安卓无法转换ppt与pptx)
Flashpaper缺少后续支持,不建议使用(后台,与安卓无关)
-
第三方付费产品中,Office Web 365
完全依赖于第三方云服务,在安全性、灵活性、稳定性为验证,不建议使用
科瀚的SOAOffice和卓正软件的pageoffice需要浏览器Activex插件的支持,对用户不是很友好,不建议试用(后台,与安卓无关)
OpenOffice的预览效果稍差,但集成方便;
Office Web Apps预览效果最佳,钉钉、126等也采用此方式,但估计集成难度稍大,另外钉钉的预览偶尔也出现不稳定的情况;
永中office的预览效果和集成难度比较平衡,但需付费。
总之,除非需求必须,否则这种功能还是不要实现,因为难度很大,耗时费力,一般情况下交给系统去打开就行了。当然,主要功能是文档阅读的app除外。
一些代码参考
说明:大部分是从网上找的,仅供参考,功能没问题,但很多细节有待完善。
package com.ght.loginapp.utilities;
import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
/**
* 2016/9/12 0012,由 yuezc 创建 .
* <p/>
* 功能描述:
* <p/>
* 说明:
* ---------------------------
* 修改时间:
* 修改说明:
* 修改人:
* <p/>
* POIFS
* POIFS是该项目的最古老,最稳定的一部分。.这是格式化OLE 2复合文档为纯Java的接口。 它同时支持读写功能。所有的组件,最终都依赖于它的定义
* <p/>
* HSSF 和 XSSF
* HSSF: MS-Excel 97-2003(.xls),基于BIFF8格式的JAVA接口。
* XSSF:MS-Excel 2007+(.xlsx),基于OOXML格式的JAVA接口。
* <p/>
* HWPF 和XWPF
* HWPF: MS-Word 97-2003(.doc),基于BIFF8格式的JAVA接口。只支持.doc文件简单的操作,读写能力有限。本API为POI项目早期开发,很不幸的 是主要负责HWPF模块开发的工程师-“Ryan Ackley”已经离开Apache组织,现在该模块没有人维护、更新、完善。
* XWPF:MS-Word 2007+(.docx),基于OOXML格式的JAVA接口。较HWPF功能完善。
*/
public class DocumentFormatConvertUtils {
/**
* doc文档转成html格式
*/
public static void doc2html(String docPath, final String docName, String htmlName, String htmlPath) {
HWPFDocument wordDocument = null;
try {
wordDocument = new HWPFDocument(new FileInputStream(docPath + docName));
} catch (IOException e) {
e.printStackTrace();
}
WordToHtmlConverter wordToHtmlConverter = null;
try {
wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//设置图片路径
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content,
PictureType pictureType, String suggestedName,
float widthInches, float heightInches) {
String name = docName.substring(0, docName.indexOf("\\."));
return name + "/" + suggestedName;
}
});
//保存图片
List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
System.out.println(pic.suggestFullFileName());
try {
String name = docName.substring(0, docName.indexOf("\\."));
pic.writeImageContent(new FileOutputStream(htmlPath + name + "/"
+ pic.suggestFullFileName()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = null;
try {
serializer = tf.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
try {
serializer.transform(domSource, streamResult);
} catch (TransformerException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
//保存html文件
writeFile(new String(out.toByteArray()), htmlPath + htmlName);
}
/**
* docx文档转成html格式
*/
public static void docx2html(String docxPath, String docxName, String htmlName, String htmlPath) {
try {
final String file = docxPath + docxName;
File f = new File(file);
if (!f.exists()) {
System.out.println("Sorry File does not Exists!");
} else {
if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {
// 1) 加载word文档生成 XWPFDocument对象
InputStream in = new FileInputStream(f);
XWPFDocument document = new XWPFDocument(in);
// 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
File imageFolderFile = new File(docxPath);
XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
options.setExtractor(new FileImageExtractor(imageFolderFile));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
// 3) 将 XWPFDocument转换成XHTML
OutputStream out = new FileOutputStream(new File(htmlPath + htmlName));
XHTMLConverter.getInstance().convert(document, out, options);
} else {
System.out.println("Enter only MS Office 2007+ files");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* xls文档转成html格式
*/
public static void xls2html(String xlsPath, String xlsName, String htmlName, String htmlPath) {
try {
InputStream input = new FileInputStream(xlsPath + xlsName);
HSSFWorkbook excelBook = new HSSFWorkbook(input);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
excelToHtmlConverter.processWorkbook(excelBook);
List pics = excelBook.getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
try {
pic.writeImageContent(new FileOutputStream(xlsName.substring(0, xlsName.indexOf(".")) + pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = excelToHtmlConverter.getDocument();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
outStream.close();
//保存html文件
writeFile(new String(outStream.toByteArray()), htmlPath + htmlName);
} catch (Exception e) {
}
}
/**
* xlsx文档转成html格式
*/
public static void xlsx2html(String xlsxPath, String xlsxName, String htmlName, String htmlPath) {
writeFile(readExcelToHtml(xlsxPath + xlsxName, true), htmlPath + htmlName);
}
public static void ppt2html(String pptPath, String pptName, String htmlName, String htmlPath) {
//抱歉,没有实现
}
public static void pptx2html(String pptxPath, String pptxName, String htmlName, String htmlPath) {
//抱歉,没有实现
}
---------------------------------------
/**
* 将html文件保存到sd卡
*/
public static void writeFile(String content, String path) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
} catch (IOException ie) {
}
}
}
/**
* @param filePath 文件的路径
* @param isWithStyle 是否需要表格样式 包含 字体 颜色 边框 对齐方式
* @return <table>
* ...
* </table>
* 字符串
*/
public static String readExcelToHtml(String filePath, boolean isWithStyle) {
InputStream is = null;
String htmlExcel = null;
try {
File sourcefile = new File(filePath);
is = new FileInputStream(sourcefile);
Workbook wb = WorkbookFactory.create(is);
if (wb instanceof XSSFWorkbook) {
XSSFWorkbook xWb = (XSSFWorkbook) wb;
htmlExcel = getExcelInfo(xWb, isWithStyle);
} else if (wb instanceof HSSFWorkbook) {
HSSFWorkbook hWb = (HSSFWorkbook) wb;
htmlExcel = getExcelInfo(hWb, isWithStyle);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return htmlExcel;
}
public static String getExcelInfo(Workbook wb, boolean isWithStyle) {
StringBuffer sb = new StringBuffer();
Sheet sheet = wb.getSheetAt(0);// 获取第一个Sheet的内容
int lastRowNum = sheet.getLastRowNum();
Map<String, String> map[] = getRowSpanColSpanMap(sheet);
sb.append("<table style='border-collapse:collapse;' width='100%'>");
Row row = null; // 兼容
Cell cell = null; // 兼容
for (int rowNum = sheet.getFirstRowNum(); rowNum <= lastRowNum; rowNum++) {
row = sheet.getRow(rowNum);
if (row == null) {
sb.append("<tr><td > </td></tr>");
continue;
}
sb.append("<tr>");
int lastColNum = row.getLastCellNum();
for (int colNum = 0; colNum < lastColNum; colNum++) {
cell = row.getCell(colNum);
if (cell == null) { // 特殊情况 空白的单元格会返回null
sb.append("<td> </td>");
continue;
}
String stringValue = getCellValue(cell);
if (map[0].containsKey(rowNum + "," + colNum)) {
String pointString = map[0].get(rowNum + "," + colNum);
map[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 + "' ");
} else if (map[1].containsKey(rowNum + "," + colNum)) {
map[1].remove(rowNum + "," + colNum);
continue;
} else {
sb.append("<td ");
}
// 判断是否需要样式
if (isWithStyle) {
dealExcelStyle(wb, sheet, cell, sb);// 处理单元格样式
}
sb.append(">");
if (stringValue == null || "".equals(stringValue.trim())) {
sb.append(" ");
} else {
// 将ascii码为160的空格转换为html下的空格( )
sb.append(stringValue.replace(String.valueOf((char) 160), " "));
}
sb.append("</td>");
}
sb.append("</tr>");
}
sb.append("</table>");
return sb.toString();
}
private static Map<String, String>[] getRowSpanColSpanMap(Sheet sheet) {
Map<String, String> map0 = new HashMap<String, String>();
Map<String, String> map1 = new HashMap<String, String>();
int mergedNum = sheet.getNumMergedRegions();
CellRangeAddress range = null;
for (int i = 0; i < mergedNum; i++) {
range = 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;
while (tempRow <= bottomRow) {
int tempCol = topCol;
while (tempCol <= bottomCol) {
map1.put(tempRow + "," + tempCol, "");
tempCol++;
}
tempRow++;
}
map1.remove(topRow + "," + topCol);
}
Map[] map = {map0, map1};
return map;
}
/**
* 获取表格单元格Cell内容
*
* @param cell
* @return
*/
private static String getCellValue(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
break;
case Cell.CELL_TYPE_STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case Cell.CELL_TYPE_BLANK:
result = "";
break;
default:
result = "";
break;
}
return result;
}
/**
* 处理表格样式
*
* @param wb
* @param sheet
* @param cell
* @param sb
*/
private static void dealExcelStyle(Workbook wb, Sheet sheet, Cell cell, StringBuffer sb) {
CellStyle cellStyle = cell.getCellStyle();
if (cellStyle != null) {
short alignment = cellStyle.getAlignment();
sb.append("align='" + convertAlignToHtml(alignment) + "' ");// 单元格内容的水平对齐方式
short verticalAlignment = cellStyle.getVerticalAlignment();
sb.append("valign='" + convertVerticalAlignToHtml(verticalAlignment) + "' ");// 单元格中内容的垂直排列方式
if (wb instanceof XSSFWorkbook) {
XSSFFont xf = ((XSSFCellStyle) cellStyle).getFont();
short boldWeight = xf.getBoldweight();
sb.append("style='");
sb.append("font-weight:" + boldWeight + ";"); // 字体加粗
sb.append("font-size: " + xf.getFontHeight() / 2 + "%;"); // 字体大小
int columnWidth = sheet.getColumnWidth(cell.getColumnIndex());
sb.append("width:" + columnWidth + "px;");
XSSFColor xc = xf.getXSSFColor();
if (xc != null && !"".equals(xc)) {
sb.append("color:#" + xc.getARGBHex().substring(2) + ";"); // 字体颜色
}
XSSFColor bgColor = (XSSFColor) cellStyle.getFillForegroundColorColor();
// System.out.println("************************************");
// System.out.println("BackgroundColorColor:
// "+cellStyle.getFillBackgroundColorColor());
// System.out.println("ForegroundColor:
// "+cellStyle.getFillForegroundColor());//0
// System.out.println("BackgroundColorColor:
// "+cellStyle.getFillBackgroundColorColor());
// System.out.println("ForegroundColorColor:
// "+cellStyle.getFillForegroundColorColor());
// String bgColorStr = bgColor.getARGBHex();
// System.out.println("bgColorStr: "+bgColorStr);
if (bgColor != null && !"".equals(bgColor)) {
sb.append("background-color:#" + bgColor.getARGBHex().substring(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()));
} else if (wb instanceof HSSFWorkbook) {
HSSFFont hf = ((HSSFCellStyle) cellStyle).getFont(wb);
short boldWeight = hf.getBoldweight();
short fontColor = hf.getColor();
sb.append("style='");
HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette(); // 类HSSFPalette用于求的颜色的国际标准形式
HSSFColor hc = palette.getColor(fontColor);
sb.append("font-weight:" + boldWeight + ";"); // 字体加粗
sb.append("font-size: " + hf.getFontHeight() / 2 + "%;"); // 字体大小
String fontColorStr = convertToStardColor(hc);
if (fontColorStr != null && !"".equals(fontColorStr.trim())) {
sb.append("color:" + fontColorStr + ";"); // 字体颜色
}
int columnWidth = sheet.getColumnWidth(cell.getColumnIndex());
sb.append("width:" + columnWidth + "px;");
short bgColor = cellStyle.getFillForegroundColor();
hc = palette.getColor(bgColor);
String bgColorStr = convertToStardColor(hc);
if (bgColorStr != null && !"".equals(bgColorStr.trim())) {
sb.append("background-color:" + bgColorStr + ";"); // 背景颜色
}
sb.append(getBorderStyle(palette, 0, cellStyle.getBorderTop(), cellStyle.getTopBorderColor()));
sb.append(getBorderStyle(palette, 1, cellStyle.getBorderRight(), cellStyle.getRightBorderColor()));
sb.append(getBorderStyle(palette, 3, cellStyle.getBorderLeft(), cellStyle.getLeftBorderColor()));
sb.append(getBorderStyle(palette, 2, cellStyle.getBorderBottom(), cellStyle.getBottomBorderColor()));
}
sb.append("' ");
}
}
/**
* 单元格内容的水平对齐方式
*
* @param alignment
* @return
*/
private static String convertAlignToHtml(short alignment) {
String align = "left";
switch (alignment) {
case CellStyle.ALIGN_LEFT:
align = "left";
break;
case CellStyle.ALIGN_CENTER:
align = "center";
break;
case CellStyle.ALIGN_RIGHT:
align = "right";
break;
default:
break;
}
return align;
}
/**
* 单元格中内容的垂直排列方式
*
* @param verticalAlignment
* @return
*/
private static String convertVerticalAlignToHtml(short verticalAlignment) {
String valign = "middle";
switch (verticalAlignment) {
case CellStyle.VERTICAL_BOTTOM:
valign = "bottom";
break;
case CellStyle.VERTICAL_CENTER:
valign = "center";
break;
case CellStyle.VERTICAL_TOP:
valign = "top";
break;
default:
break;
}
return valign;
}
private static String convertToStardColor(HSSFColor hc) {
StringBuffer sb = new StringBuffer("");
if (hc != null) {
if (HSSFColor.AUTOMATIC.index == hc.getIndex()) {
return null;
}
sb.append("#");
for (int i = 0; i < hc.getTriplet().length; i++) {
sb.append(fillWithZero(Integer.toHexString(hc.getTriplet()[i])));
}
}
return sb.toString();
}
private static String fillWithZero(String str) {
if (str != null && str.length() < 2) {
return "0" + str;
}
return str;
}
static String[] bordesr = {"border-top:", "border-right:", "border-bottom:", "border-left:"};
static String[] borderStyles = {"solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid ",
"solid ", "solid", "solid", "solid", "solid", "solid"};
private static String getBorderStyle(HSSFPalette palette, int b, short s, short t) {
if (s == 0)
return bordesr[b] + borderStyles[s] + "#d0d7e5 1px;";
String borderColorStr = convertToStardColor(palette.getColor(t));
borderColorStr = borderColorStr == null || borderColorStr.length() < 1 ? "#000000" : borderColorStr;
return bordesr[b] + borderStyles[s] + borderColorStr + " 1px;";
}
private static String getBorderStyle(int b, short s, XSSFColor xc) {
if (s == 0)
return bordesr[b] + borderStyles[s] + "#d0d7e5 1px;";
if (xc != null && !"".equals(xc)) {
String borderColorStr = xc.getARGBHex();// t.getARGBHex();
borderColorStr = borderColorStr == null || borderColorStr.length() < 1 ? "#000000"
: borderColorStr.substring(2);
return bordesr[b] + borderStyles[s] + borderColorStr + " 1px;";
}
return "";
}
}