ASF Bugzilla – Attachment 34273 Details for
Bug 60147
Unable to export string “IBM_x3550_M2” correctly
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
code sample
XlsxUtils.java (text/x-csrc), 16.24 KB, created by
onlyarticle
on 2016-09-19 08:34:51 UTC
(
hide
)
Description:
code sample
Filename:
MIME Type:
Creator:
onlyarticle
Created:
2016-09-19 08:34:51 UTC
Size:
16.24 KB
patch
obsolete
>/** > * @(#)XlsxFileUtils.java 2016å¹´8æ11æ¥ > * > * Copyright 2000-2013 by ChinaNetCenter Corporation. > * > * All rights reserved. > * > * This software is the confidential and proprietary information of > * ChinaNetCenter Corporation ("Confidential Information"). You > * shall not disclose such Confidential Information and shall use > * it only in accordance with the terms of the license agreement > * you entered into with ChinaNetCenter. > * > */ > >package com.cnc.rmp.util; > >import java.awt.Color; >import java.io.ByteArrayInputStream; >import java.io.ByteArrayOutputStream; >import java.io.IOException; >import java.io.InputStream; >import java.io.OutputStream; >import java.text.SimpleDateFormat; >import java.util.Collection; >import java.util.Date; >import java.util.Iterator; >import java.util.List; >import java.util.Map; >import java.util.regex.Matcher; >import java.util.regex.Pattern; > >import org.apache.poi.hssf.usermodel.HSSFCellStyle; >import org.apache.poi.hssf.util.HSSFColor; >import org.apache.poi.ss.util.CellRangeAddress; >import org.apache.poi.xssf.usermodel.XSSFCell; >import org.apache.poi.xssf.usermodel.XSSFCellStyle; >import org.apache.poi.xssf.usermodel.XSSFColor; >import org.apache.poi.xssf.usermodel.XSSFFont; >import org.apache.poi.xssf.usermodel.XSSFRichTextString; >import org.apache.poi.xssf.usermodel.XSSFRow; >import org.apache.poi.xssf.usermodel.XSSFSheet; >import org.apache.poi.xssf.usermodel.XSSFWorkbook; >import org.slf4j.Logger; >import org.slf4j.LoggerFactory; > >import com.cnc.portal.exception.GenericException; >import com.cnc.portal.util.CollectionUtil; >import com.cnc.portal.util.StringUtil; >import com.cnc.rmp.vo.entity.excel.ExcelTableEntity; > >/** > * xlsx è¡¨æ ¼æä½ç±». > * > * @author suyc@chinanetcenter.com > * @date 2016å¹´8æ11æ¥ > * @version 1.0.0 > */ >public class XlsxUtils extends com.cnc.commons.utils.FileUtils { > > private final static Logger LOGGER = LoggerFactory > .getLogger(XlsxUtils.class); > > private static final int sheetMaxSize = 60000; > > public final static short SKY_BLUE = 0x28; > public final static short LIGHT_YELLOW = 0x2b; > public final static short WHITE = 0x9; > > private XlsxUtils() { > > } > > public static void buildExcelHeader(XSSFWorkbook workbook, XSSFSheet sheet, > XSSFCellStyle style, String[] headerName) { > XSSFRow row = sheet.createRow(0); > setCellStyle4Title(workbook, style); > for (int i = 0; i < headerName.length; i++) { > XSSFCell cell = row.createCell(i); > cell.setCellStyle(style); > XSSFRichTextString text = new XSSFRichTextString(headerName[i]); > cell.setCellValue(text); > } > } > > public static void buildExcelHeader(XSSFWorkbook workbook, XSSFSheet sheet, > XSSFCellStyle style, List<List<String>> headerss, > List<List<Integer>> indexss) { > > style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// åç´å± ä¸ > style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// æ°´å¹³å± ä¸ > setCellStyle4Title(workbook, style); > > for (int i = 0; i < headerss.size(); i++) { > XSSFRow row = sheet.createRow(i); > List<String> headers = headerss.get(i); > List<Integer> indexs = indexss.get(i); > for (int j = 0; j < headers.size(); j++) { > XSSFCell cell = row.createCell(indexs.get(j)); > cell.setCellStyle(style); > XSSFRichTextString text = new XSSFRichTextString(headers.get(j)); > cell.setCellValue(text); > } > } > } > > public static void buildExcelRow(XSSFWorkbook workbook, XSSFFont font, > XSSFSheet sheet, XSSFCellStyle style, int rowNum, String[] colkeys, > Map<String, Object> record) { > XSSFRow row = sheet.createRow(rowNum); > setCellStyle4Row(workbook, style, font); > String textValue = StringUtil.EMPTY; > String pattern = "yyyy-MM-dd"; > for (int i = 0; i < colkeys.length; i++) { > XSSFCell cell = row.createCell(i); > cell.setCellStyle(style); > Object value = record.get(colkeys[i]); > if (value instanceof Date) { > Date date = (Date) value; > SimpleDateFormat sdf = new SimpleDateFormat(pattern); > textValue = sdf.format(date); > } else { > textValue = StringUtil.toString(value); > } > // å©ç¨æ£å表达å¼å¤ætextValueæ¯å¦å ¨é¨ç±æ°åç»æ > if (StringUtil.isNotEmpty(textValue)) { > Pattern p = Pattern.compile("^//d+(//.//d+)?$"); > Matcher matcher = p.matcher(textValue); > if (matcher.matches()) { > // æ¯æ°åå½ä½doubleå¤ç > cell.setCellValue(Double.parseDouble(textValue)); > } else { > XSSFRichTextString richString = new XSSFRichTextString( > textValue); > richString.applyFont(font); > cell.setCellValue(richString); > } > > } > } > } > > public static InputStream exportXLS(ExcelTableEntity excel) > throws IOException { > if (CommonUtils.isNull(excel)) { > return null; > } > ByteArrayOutputStream out = new ByteArrayOutputStream(); > exportXLS(excel, out); > if ((out.toByteArray()) != null && (out.toByteArray().length > 0)) { > InputStream in = new ByteArrayInputStream(out.toByteArray()); > return in; > } > return null; > } > > public static InputStream exportXLS(List<ExcelTableEntity> excels) > throws IOException { > if (CollectionUtil.isEmpty(excels)) { > return null; > } > ByteArrayOutputStream out = new ByteArrayOutputStream(); > exportXLS(excels, out); > if ((out.toByteArray()) != null && (out.toByteArray().length > 0)) { > InputStream in = new ByteArrayInputStream(out.toByteArray()); > return in; > } > return null; > } > > public static void exportXLS(ExcelTableEntity excel, OutputStream out) > throws IOException { > try { > XSSFWorkbook workbook = new XSSFWorkbook(); > > int rowNum = 1; > int sheetNum = 1; > String title = excel.getTitle(); > // çæä¸ä¸ªè¡¨æ ¼ > XSSFSheet sheet = workbook.createSheet(title); > // å¦æ没æ设置åå宽度ï¼åé»è®¤ä¸º15ï¼å¦ææï¼å设置åå宽度 > Map<Integer, Integer> map = excel.getWidthMap(); > if (map.isEmpty()) { > sheet.setDefaultColumnWidth(excel.getDefaultColumnWidth()); > } else { > if (excel.getColumns() != null) { > int length = excel.getColumns().length; > for (int i = 0; i < length; i++) { > sheet.setColumnWidth(i, > excel.getDefaultColumnWidth() * 256); > } > } > > for (Integer key : map.keySet()) { > sheet.setColumnWidth(key, map.get(key) * 256); > } > } > > String[] headerNames = excel.getHeaders(); > XSSFCellStyle style = workbook.createCellStyle();// XSSFCellStyleå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > style.setWrapText(true); > buildExcelHeader(workbook, sheet, style, headerNames); > String[] colkeys = excel.getColumns(); > Collection<Map<String, Object>> rows = excel.getRows(); > if (rows != null && rows.size() > 0) { > XSSFFont font = workbook.createFont();// XSSFFontå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > font.setColor(new XSSFColor(Color.BLACK)); > style = workbook.createCellStyle();// XSSFCellStyleå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > for (Iterator<Map<String, Object>> it = rows.iterator(); it > .hasNext();) { > Map<String, Object> tmp = it.next(); > buildExcelRow(workbook, font, sheet, style, rowNum++, > colkeys, tmp); > if (rowNum > sheetMaxSize) { > rowNum = 1; > sheetNum++; > sheet = workbook.createSheet(title + " " + sheetNum); > // è®¾ç½®è¡¨æ ¼é»è®¤å宽度为15个åè(éé¦ä¸ªsheet) > sheet.setDefaultColumnWidth(excel > .getDefaultColumnWidth()); > style = workbook.createCellStyle(); > buildExcelHeader(workbook, sheet, style, headerNames); > } > } > } > workbook.write(out); > } catch (Exception e) { > if (LOGGER.isErrorEnabled()) { > LOGGER.error(e.getMessage(), e); > } > throw new GenericException("rmp.validator.error.report", > e.getCause()); > } > > } > > public static void exportXLS(List<ExcelTableEntity> excels, OutputStream out) > throws IOException { > try { > XSSFWorkbook workbook = new XSSFWorkbook(); > for (ExcelTableEntity excel : excels) { > int rowNum = 1; > if (CollectionUtil.isNotEmpty(excel.getHeaderss())) { > rowNum = excel.getHeaderss().size(); > } > int sheetNum = 1; > String title = excel.getTitle(); > // çæä¸ä¸ªè¡¨æ ¼ > XSSFSheet sheet = workbook.createSheet(title); > if (CollectionUtil.isNotEmpty(excel.getMergedRegionss())) { > mergedCell(sheet, excel.getMergedRegionss()); > } > > // è®¾ç½®è¡¨æ ¼é»è®¤å宽度为15个åè > sheet.setDefaultColumnWidth(excel.getDefaultColumnWidth()); > String[] headerNames = excel.getHeaders(); > XSSFCellStyle style = workbook.createCellStyle();// XSSFCellStyleå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > if (CollectionUtil.isNotEmpty(excel.getHeaderss())) { > buildExcelHeader(workbook, sheet, style, > excel.getHeaderss(), excel.getIndexss()); > } else { > buildExcelHeader(workbook, sheet, style, headerNames); > } > String[] colkeys = excel.getColumns(); > Collection<Map<String, Object>> rows = excel.getRows(); > if (rows != null && rows.size() > 0) { > XSSFFont font = workbook.createFont();// XSSFFontå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > font.setColor(new XSSFColor(Color.BLACK)); > style = workbook.createCellStyle();// XSSFCellStyleå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > for (Iterator<Map<String, Object>> it = rows.iterator(); it > .hasNext();) { > Map<String, Object> tmp = it.next(); > buildExcelRow(workbook, font, sheet, style, rowNum++, > colkeys, tmp); > if (rowNum > sheetMaxSize) { > rowNum = 1; > sheetNum++; > sheet = workbook > .createSheet(title + " " + sheetNum); > // è®¾ç½®è¡¨æ ¼é»è®¤å宽度为15个åè(éé¦ä¸ªsheet) > sheet.setDefaultColumnWidth(excel > .getDefaultColumnWidth()); > style = workbook.createCellStyle(); > buildExcelHeader(workbook, sheet, style, > headerNames); > } > } > } > } > workbook.write(out); > } catch (Exception e) { > if (LOGGER.isErrorEnabled()) { > LOGGER.error(e.getMessage(), e); > } > throw new GenericException("rmp.validator.error.report", > e.getCause()); > } > > } > > //å并åå æ ¼ > private static void mergedCell(XSSFSheet sheet, > List<List<Integer>> mergedRegionss) { > for (int i = 0; i < mergedRegionss.size(); i++) { > List<Integer> mergedRegions = mergedRegionss.get(i); > sheet.addMergedRegion(new CellRangeAddress(mergedRegions.get(0), mergedRegions.get(1), mergedRegions.get(2), mergedRegions.get(3))); > } > } > > private static void setCellStyle4Title(XSSFWorkbook workbook, > XSSFCellStyle style) { > // 设置è¿äºæ ·å¼ > style.setFillForegroundColor(SKY_BLUE); > style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); > style.setBorderBottom(XSSFCellStyle.BORDER_THIN); > style.setBorderLeft(XSSFCellStyle.BORDER_THIN); > style.setBorderRight(XSSFCellStyle.BORDER_THIN); > style.setBorderTop(XSSFCellStyle.BORDER_THIN); > style.setAlignment(XSSFCellStyle.ALIGN_CENTER); > // çæä¸ä¸ªåä½ > XSSFFont font = workbook.createFont(); > font.setColor(new XSSFColor(Color.BLACK)); > font.setFontHeightInPoints((short) 12); > font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); > // æåä½åºç¨å°å½åçæ ·å¼ > style.setFont(font); > } > > private static void setCellStyle4Row(XSSFWorkbook workbook, > XSSFCellStyle style, XSSFFont font) { > // çæ并设置å¦ä¸ä¸ªæ ·å¼ > style.setFillForegroundColor(new XSSFColor(Color.WHITE)); > style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); > style.setBorderBottom(XSSFCellStyle.BORDER_THIN); > style.setBorderLeft(XSSFCellStyle.BORDER_THIN); > style.setBorderRight(XSSFCellStyle.BORDER_THIN); > style.setBorderTop(XSSFCellStyle.BORDER_THIN); > style.setAlignment(XSSFCellStyle.ALIGN_LEFT); > style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); > style.setWrapText(true); > // æåä½åºç¨å°å½åçæ ·å¼ > style.setFont(font); > } > > /** > * ****************以ä¸ä¸ºåæ¥ExcelUtils çå 容******************************** > */ > public static void exportXLS(ExcelTableEntity excel, XSSFWorkbook workbook) > throws IOException { > SheetProcessor processor = null; > exportXLS(excel, workbook, processor); > } > > public static void exportXLS(ExcelTableEntity excel, XSSFWorkbook workbook, > SheetProcessor processor) throws IOException { > try { > int rowNum = 1; > int sheetNum = 1; > String title = excel.getTitle(); > // çæä¸ä¸ªè¡¨æ ¼ > XSSFSheet sheet = workbook.createSheet(title); > // è®¾ç½®è¡¨æ ¼é»è®¤å宽度为15个åè > sheet.setDefaultColumnWidth(15); > String[] headerNames = excel.getHeaders(); > XSSFCellStyle style = workbook.createCellStyle();// HSSFCellStyleå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > XSSFCellStyle deviceStyle = workbook.createCellStyle();// HSSFCellStyleå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > buildExcelHeader(workbook, sheet, style, headerNames); > String[] colkeys = excel.getColumns(); > Collection<Map<String, Object>> rows = excel.getRows(); > if (rows != null && rows.size() > 0) { > XSSFFont font = workbook.createFont();// HSSFFontå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > font.setColor(HSSFColor.BLACK.index); > style = workbook.createCellStyle();// HSSFCellStyleå¨å¤é¢å®ä¹ï¼å ¬ç¨ä¸ä¸ª > for (Iterator<Map<String, Object>> it = rows.iterator(); it > .hasNext();) { > Map<String, Object> tmp = it.next(); > buildExcelRow(workbook, font, sheet, style, deviceStyle, > rowNum++, colkeys, tmp); > if (rowNum > 60000) { > sheet.createFreezePane(1, 0); > rowNum = 1; > sheetNum++; > sheet = workbook.createSheet(title + " " + sheetNum); > // è®¾ç½®è¡¨æ ¼é»è®¤å宽度为15个åè(éé¦ä¸ªsheet) > sheet.setDefaultColumnWidth(15); > style = workbook.createCellStyle(); > buildExcelHeader(workbook, sheet, style, headerNames); > } > } > // sheet.createFreezePane(1, 0); > > if (processor != null) { > processor.process(sheet); > } > } > // workbook.write(out); > } catch (Exception e) { > if (LOGGER.isErrorEnabled()) { > LOGGER.error(e.getMessage(), e); > } > throw new GenericException("rmp.validator.error.report", > e.getCause()); > } > > } > > private static void buildExcelRow(XSSFWorkbook workbook, XSSFFont font, > XSSFSheet sheet, XSSFCellStyle style, XSSFCellStyle deviceStyle, > int rowNum, String[] colkeys, Map<String, Object> record) { > XSSFRow row = sheet.createRow(rowNum); > boolean isNormalStyle = true; > if (!isNormalStyle) { > setCellStyle4Row(workbook, deviceStyle, font, isNormalStyle); > } else { > setCellStyle4Row(workbook, style, font, isNormalStyle); > } > String textValue = StringUtil.EMPTY; > String pattern = "yyyy-MM-dd"; > for (int i = 0; i < colkeys.length; i++) { > XSSFCell cell = row.createCell(i); > if (isNormalStyle) { > cell.setCellStyle(style); > } else { > cell.setCellStyle(deviceStyle); > } > Object value = record.get(colkeys[i]); > if (value instanceof Date) { > Date date = (Date) value; > SimpleDateFormat sdf = new SimpleDateFormat(pattern); > textValue = sdf.format(date); > } else { > textValue = StringUtil.toString(value); > } > // å©ç¨æ£å表达å¼å¤ætextValueæ¯å¦å ¨é¨ç±æ°åç»æ > if (StringUtil.isNotEmpty(textValue)) { > Pattern p = Pattern.compile("^//d+(//.//d+)?$"); > Matcher matcher = p.matcher(textValue); > if (matcher.matches()) { > // æ¯æ°åå½ä½doubleå¤ç > cell.setCellValue(Double.parseDouble(textValue)); > } else { > XSSFRichTextString richString = new XSSFRichTextString( > textValue); > richString.applyFont(font); > cell.setCellValue(richString); > } > > } > } > } > > private static void setCellStyle4Row(XSSFWorkbook workbook, > XSSFCellStyle style, XSSFFont font, Boolean isServer) { > // çæ并设置å¦ä¸ä¸ªæ ·å¼ > if (!isServer) { > style.setFillForegroundColor(LIGHT_YELLOW); > } else { > style.setFillForegroundColor(WHITE); > } > style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); > style.setBorderBottom(HSSFCellStyle.BORDER_THIN); > style.setBorderLeft(HSSFCellStyle.BORDER_THIN); > style.setBorderRight(HSSFCellStyle.BORDER_THIN); > style.setBorderTop(HSSFCellStyle.BORDER_THIN); > style.setAlignment(HSSFCellStyle.ALIGN_LEFT); > style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); > // æåä½åºç¨å°å½åçæ ·å¼ > style.setFont(font); > } > > public interface SheetProcessor { > public void process(XSSFSheet sheet); > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 60147
:
34272
| 34273