标签:
public static Workbook datasToWorkbook(String path, String sheetName, List<List<String>> datas, int startRow,
int startCol) {
Workbook wb = readExcel(path);
if (wb == null) {
wb = createWorkbook(false);
}
CellStyle cellStyle1 = wb.createCellStyle();
cellStyle1.setBorderTop((short) 1);
cellStyle1.setBorderBottom((short) 1);
cellStyle1.setBorderLeft((short) 1);
cellStyle1.setBorderRight((short) 1);
cellStyle1.setAlignment((short) 2);
cellStyle1.setFillForegroundColor(IndexedColors.WHITE.getIndex());
cellStyle1.setFillPattern((short) 1);
cellStyle1.setVerticalAlignment((short) 1);
CellStyle cellStyle2 = wb.createCellStyle();
cellStyle2.setBorderTop((short) 1);
cellStyle2.setBorderBottom((short) 1);
cellStyle2.setBorderLeft((short) 1);
cellStyle2.setBorderRight((short) 1);
cellStyle2.setAlignment((short) 2);
cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex());
cellStyle2.setFillPattern((short) 1);
cellStyle2.setVerticalAlignment((short) 1);
cellStyle2.setWrapText(true);
HSSFFont font = (HSSFFont) wb.createFont();
font.setBoldweight((short) 700);
CellStyle cellStyle3 = wb.createCellStyle();
cellStyle3.setBorderTop((short) 1);
cellStyle3.setBorderBottom((short) 1);
cellStyle3.setBorderLeft((short) 1);
cellStyle3.setBorderRight((short) 1);
cellStyle3.setAlignment((short) 2);
cellStyle3.setFillForegroundColor(IndexedColors.WHITE.getIndex());
cellStyle3.setFillPattern((short) 1);
cellStyle3.setVerticalAlignment((short) 1);
cellStyle3.setFont(font);
Sheet sheet = wb.getSheet(sheetName);
if (sheet != null) {
int index = wb.getSheetIndex(sheetName);
wb.removeSheetAt(index);
}
sheet = wb.createSheet(sheetName);
int colNums = 0;
for (int i = startRow; i < datas.size() + startRow; i++) {
Row row = sheet.createRow(i);
List<String> rowData = datas.get(i-startRow);
// if (colNums != rowData.size()) {
// throw new RuntimeException(
// "row#" + i + " wrong col number!expected " + colNums + " but actually " + rowData.size());
// }
for (int j = startCol; j < rowData.size() + startCol; j++) {
Cell cell = row.createCell(j);
String val = (rowData.get(j)).trim();
val = val == null ? "" : val;
if (val.length() > 1000) {
val = val.substring(0, 1000) + "\r\n......";
}
if ("N".equals(val))
cell.setCellStyle(cellStyle2);
else {
cell.setCellStyle(cellStyle1);
}
cell.setCellValue(val);
}
}
for (int j = 0; j < datas.get(0).size(); j++) {
sheet.setColumnWidth(j, 6000);
}
return wb;
}
public static Workbook createWorkbook(boolean isExcel2007)
{
Workbook wb = null;
wb = isExcel2007 ? new XSSFWorkbook() : new HSSFWorkbook();
return wb;
}
public static Workbook readExcel(String filePath)
{
InputStream is = null;
Workbook wb = null;
try {
is = new FileInputStream(filePath);
wb = WorkbookFactory.create(is);
} catch (RuntimeException e) {
throw e;
}
catch (Exception localException)
{
try {
if (is != null)
is.close();
}
catch (IOException e) {
throw new RuntimeException("Failed to close InputStream", e);
}
}
finally
{
try
{
if (is != null)
is.close();
}
catch (IOException e) {
throw new RuntimeException("Failed to close InputStream", e);
}
}
return wb;
}
标签:
原文地址:http://www.cnblogs.com/zhangxuesong/p/5533396.html