導(dǎo)除EXCEL數(shù)據(jù)

導(dǎo)出效果如下,實(shí)現(xiàn)在網(wǎng)頁彈出選擇保存路徑,可以合并單元格,設(shè)置字體信息,單元格行寬、列高。

private void exportExcel(String parentTitle, String sheetName,

String[] headers, String[] fields, Collection<T> dataset,

OutputStream out, String pattern, String date, int[] columnWidth) {

// 聲明一個(gè)工作薄

HSSFWorkbook workbook = new HSSFWorkbook();

// 生成一個(gè)表格

HSSFSheet sheet = workbook.createSheet(sheetName);

// 設(shè)置表格默認(rèn)列寬度為15個(gè)字節(jié)

// sheet.setDefaultColumnWidth((short) 18);

// 設(shè)置表格列寬,列寬計(jì)算公式為(int)((columnWidth+0.72)*256)

for (int i = 0; i < columnWidth.length; i++) {

sheet.setColumnWidth(i, (int) ((columnWidth[i] + 0.72) * 256));

}

// 聲明一個(gè)畫圖的頂級管理器

HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

// 定義注釋的大小和位置,詳見文檔

HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,

0, 0, 0, (short) 4, 2, (short) 6, 5));

// 設(shè)置注釋內(nèi)容

comment.setString(new HSSFRichTextString(parentTitle));

// 設(shè)置注釋作者,當(dāng)鼠標(biāo)移動(dòng)到單元格上是可以在狀態(tài)欄中看到該內(nèi)容.

comment.setAuthor("billz");

// 主標(biāo)題

HSSFRow pTitleRow = sheet.createRow(0);

HSSFCell pTitleCell = pTitleRow.createCell(0);

pTitleCell.setCellStyle(getParentTitleStyle(workbook));

pTitleCell.setCellValue(new HSSFRichTextString(parentTitle));

// 跨第1行第1個(gè)到第2個(gè)單元格的操作為

CellRangeAddress rg = new CellRangeAddress(0, (short) 0, 0,

(short) (headers.length - 1));

sheet.addMergedRegion(rg);

pTitleRow.setHeightInPoints(35);// 目的是想把行高設(shè)置成35px

// end

// 顯示時(shí)間行,第二行

HSSFRow dateRow = sheet.createRow(1);

sheet.addMergedRegion(new CellRangeAddress(1, 1, headers.length - 2,

headers.length - 1));

dateRow.setHeight((short) 400);

HSSFCell dateCell = dateRow.createCell(headers.length - 2);

dateCell.setCellStyle(getDateStyle(workbook));

dateCell.setCellValue(new HSSFRichTextString(date));

// 產(chǎn)生表格標(biāo)題行

HSSFRow row = sheet.createRow(2);

row.setHeight((short) 0x279);

for (int i = 0; i < headers.length; i++) {

HSSFCell cell = row.createCell(i);

cell.setCellStyle(getTitleStyle(workbook));

HSSFRichTextString text = new HSSFRichTextString(headers[i]);

cell.setCellValue(text);

}

// 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行

Iterator<T> it = dataset.iterator();

int index = 2;

while (it.hasNext()) {

index++;

row = sheet.createRow(index);

row.setHeight((short) 512);

T t = it.next();

// 利用反射,根據(jù)javabean屬性的先后順序,動(dòng)態(tài)調(diào)用getXxx()方法得到屬性值

for (int i = 0; i < fields.length; i++) {

HSSFCell cell = row.createCell(i);

cell.setCellStyle(getValueStyle(workbook));

String fieldName = fields[i];

String getMethodName = "get"

+ fieldName.substring(0, 1).toUpperCase()

+ fieldName.substring(1);

try {

Method getMethod = t.getClass().getMethod(getMethodName,

new Class[] {});

Object value = getMethod.invoke(t, new Object[] {});

// 判斷值的類型后進(jìn)行強(qiáng)制類型轉(zhuǎn)換

String textValue = null;

if (value instanceof Boolean) {

boolean bValue = (Boolean) value;

textValue = "是";

if (!bValue) {

textValue = "否";

}

} else if (value instanceof Date) {

Date date1 = (Date) value;

SimpleDateFormat sdf = new SimpleDateFormat(pattern);

textValue = sdf.format(date1);

} else if (value instanceof byte[]) {

// 有圖片時(shí),設(shè)置行高為60px;

row.setHeightInPoints(60);

// 設(shè)置圖片所在列寬度為80px,注意這里單位的一個(gè)換算

sheet.setColumnWidth(i, (short) (35.7 * 80));

// sheet.autoSizeColumn(i);

byte[] bsValue = (byte[]) value;

HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,

1023, 255, (short) 6, index, (short) 6, index);

anchor.setAnchorType(2);

patriarch.createPicture(anchor, workbook.addPicture(

bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));

} else if (value instanceof Long) { // long類型的時(shí)間字符串轉(zhuǎn)為固定格式(yy/MM/dd

// hh:mm)String字符串

String dateStr = String.valueOf(value);

String year = dateStr.substring(0, 4) + "/";

String month = dateStr.substring(4, 6) + "/";

String day = dateStr.substring(6, 8) + " ";

String hour = dateStr.substring(8, 10) + ":";

String minute = dateStr.substring(10, 12);

textValue = year + month + day + hour + minute;

} else {

// 其它數(shù)據(jù)類型都當(dāng)作字符串簡單處理

textValue = value != null ? value.toString() : "";

}

// 如果不是圖片數(shù)據(jù),就利用正則表達(dá)式判斷textValue是否全部由數(shù)字組成

if (textValue != null) {

Pattern p = Pattern.compile("^//d+(//.//d+)?$");

Matcher matcher = p.matcher(textValue);

if (matcher.matches()) {

// 是數(shù)字當(dāng)作double處理

cell.setCellValue(Double.parseDouble(textValue));

} else {

HSSFRichTextString richString = new HSSFRichTextString(

textValue);

HSSFFont font3 = workbook.createFont();

// font3.setColor(HSSFColor.BLUE.index);

richString.applyFont(font3);

cell.setCellValue(richString);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

// 清理資源

}

}

}

try {

workbook.write(out);

} catch (IOException e) {

e.printStackTrace();

}

}

更多詳細(xì)信息請聯(lián)系作者。。。。。。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容