java hssfsheet_Java HSSFSheet.getColumnWidth方法代码示例

import org.apache.poi.hssf.usermodel.HSSFSheet; //导入方法依赖的package包/类

private void xlsToHtml() throws Throwable {

FileOutputStream output = new FileOutputStream(new File(htmlPath));

StringBuffer htmlHeaderSB = new StringBuffer();

htmlHeaderSB.append("

+ "xmlns='http://www.w3.org/TR/REC-html40'>");

htmlHeaderSB.append("

"

+ "

");

output.write(htmlHeaderSB.toString().getBytes());

HSSFSheet sheet;

HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath)); // 获整个Excel

for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {

if (workbook.getSheetAt(sheetIndex) != null) {

sheet = workbook.getSheetAt(sheetIndex);// 获得不为空的这个sheet

if (sheet != null) {

int firstRowNum = sheet.getFirstRowNum(); // 第一行

int lastRowNum = sheet.getLastRowNum(); // 最后一行

// 构造Table

output.write(("

+ "border-collapse:collapse;\">").getBytes());

for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) {

if (sheet.getRow(rowNum) != null) {// 如果行不为空,

HSSFRow row = sheet.getRow(rowNum);

short firstCellNum = row.getFirstCellNum(); // 该行的第一个单元格

short lastCellNum = row.getLastCellNum(); // 该行的最后一个单元格

int height = (int) (row.getHeight() / 15.625); // 行的高度

output.write(("

+ "margin:2px 0 2px 0;\">").getBytes());

for (short cellNum = firstCellNum; cellNum <= lastCellNum; cellNum++) { // 循环该行的每一个单元格

HSSFCell cell = row.getCell(cellNum);

if (cell != null) {

if (cell.getCellType() != HSSFCell.CELL_TYPE_BLANK) {

StringBuffer tdStyle = new StringBuffer("

+ "margin:2px 0 2px 0; ");

HSSFCellStyle cellStyle = cell.getCellStyle();

HSSFPalette palette = workbook.getCustomPalette(); // 类HSSFPalette用于求颜色的国际标准形式

HSSFColor hColor = palette.getColor(cellStyle.getFillForegroundColor());

HSSFColor hColor2 = palette.getColor(cellStyle.getFont(workbook).getColor());

String bgColor = convertToStardColor(hColor);// 背景颜色

short boldWeight = cellStyle.getFont(workbook).getBoldweight(); // 字体粗细

short fontHeight = (short) (cellStyle.getFont(workbook).getFontHeight() / 2); // 字体大小

String fontColor = convertToStardColor(hColor2); // 字体颜色

if (bgColor != null && !"".equals(bgColor.trim())) {

tdStyle.append(" background-color:");

tdStyle.append(bgColor);

tdStyle.append("; ");

}

if (fontColor != null && !"".equals(fontColor.trim())) {

tdStyle.append(" color:");

tdStyle.append(fontColor);

tdStyle.append("; ");

}

tdStyle.append(" font-weight:");

tdStyle.append(boldWeight);

tdStyle.append("; ");

tdStyle.append(" font-size: ");

tdStyle.append(fontHeight);

tdStyle.append("%;");

output.write((tdStyle + "\"").getBytes());

int width = (int) (sheet.getColumnWidth(cellNum) / 35.7); //

int cellRegionCol = getMergerCellRegionCol(sheet, rowNum, cellNum); // 合并的列(solspan)

int cellRegionRow = getMergerCellRegionRow(sheet, rowNum, cellNum);// 合并的行(rowspan)

String align = convertAlignToHtml(cellStyle.getAlignment()); //

String vAlign = convertVerticalAlignToHtml(cellStyle.getVerticalAlignment());

output.write((" align=\"" + align + "\" valign=\"" + vAlign + "\" width=\"" + width + "\" ").getBytes());

output.write((" colspan=\"" + cellRegionCol + "\" rowspan=\"" + cellRegionRow + "\"").getBytes());

output.write((">" + getCellValue(cell) + "

").getBytes());

}

}

}

output.write("

".getBytes());

}

}

output.write(("

").getBytes());

}

}

}

output.write(("").getBytes());

output.close();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值