通过poi的XSSF实现生成excel文件
生活随笔
收集整理的這篇文章主要介紹了
通过poi的XSSF实现生成excel文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
maven導(dǎo)入依賴jar包:
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.6</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.6</version></dependency>java代碼:
import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List;import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class Demo {public static void main(String[] args) {// 文件內(nèi)容List<Object[]> rows = new ArrayList<Object[]>();rows.add(new String[] { "編號", "姓名", "成績" });rows.add(new Object[] { 1001, "張三", 87.5F });rows.add(new Object[] { 1002, "李四", 99.5F });rows.add(new Object[] { 1003, "王五", null });rows.add(new Object[] { 1004, "小六", 59F });// 文件路徑String folderPath = "E:\\tmp";// 文件名稱String fileName = "學(xué)生分?jǐn)?shù)";// 生成文件new Demo().createExcelFile(rows, folderPath, fileName);}/*** 根據(jù)[文件內(nèi)容&文件路徑&文件名稱],生成一個excel文件*/private void createExcelFile(List<Object[]> rows, String folderPath, String fileName) {try {// 創(chuàng)建一個WorkbookXSSFWorkbook wb = new XSSFWorkbook();// 創(chuàng)建一個SheetXSSFSheet sheet = wb.createSheet(fileName);// 樣式1:設(shè)置列寬sheet.setColumnWidth(0, 2000); // 第一列的寬度為2000sheet.setColumnWidth(1, 3000); // 第二列的寬度為3000// 樣式1:設(shè)置單元格背景CellStyle titleStyle = wb.createCellStyle();titleStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);// 樣式1:設(shè)置單元格邊框titleStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下邊框titleStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左邊框titleStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上邊框titleStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右邊框// 樣式1:設(shè)置單元格字體Font font = wb.createFont();// font.setColor((short) 42); // 設(shè)置字體顏色font.setColor(HSSFColor.GREEN.index); // XSSFColor中未找到顏色和short數(shù)值的映射,使用HSSFColor來定位顏色的short值font.setFontName("黑體"); // 設(shè)置字體font.setFontHeightInPoints((short) 12);// 設(shè)置字體大小font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗體顯示 titleStyle.setFont(font);// 樣式1:設(shè)置單元格居中 titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 樣式2:設(shè)置單元格居中CellStyle contentStyle = wb.createCellStyle();contentStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 遍歷輸出每行for (int i = 0; i < rows.size(); i++) {// 創(chuàng)建一個rowXSSFRow row = sheet.createRow(i);// 每一行的數(shù)據(jù)Object[] rowData = rows.get(i);// 遍歷生成每個單元格for (int j = 0; j < rowData.length; j++) {// 創(chuàng)建一個cellXSSFCell cell = row.createCell(j);// 樣式:設(shè)置單元格樣式if (i == 0) {cell.setCellStyle(titleStyle);// 使用樣式1} else {cell.setCellStyle(contentStyle);// 使用樣式2 }// 如果為空,就不做設(shè)值處理if (null == rowData[j]) {continue;}// 設(shè)值處理:假設(shè)只有四種類型的數(shù)據(jù),如果還有其他類型,根據(jù)需要,做格式轉(zhuǎn)換// String類型數(shù)值if (rowData[j].getClass() == String.class) {cell.setCellValue((String) rowData[j]);}// double類型數(shù)值else if (rowData[j].getClass() == double.class || rowData[j].getClass() == Double.class) {cell.setCellValue((Double) rowData[j]);}// float類型數(shù)值else if (rowData[j].getClass() == float.class || rowData[j].getClass() == Float.class) {cell.setCellValue((Float) rowData[j]);}// integer類型數(shù)值else if (rowData[j].getClass() == int.class || rowData[j].getClass() == Integer.class) {cell.setCellValue((Integer) rowData[j]);}}}// 文件路徑String filePath = folderPath + File.separator + fileName + ".xls";// 含文件名的全路徑,如果是2007及以后的版本,后綴可用.xlsx,向前兼容File file = new File(filePath);// 如果父目錄不存在,創(chuàng)建父目錄if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}// 如果已存在,刪除舊文件if (file.exists()) {file.delete();}// 將excel內(nèi)容寫入到文件當(dāng)中 file.createNewFile();FileOutputStream fileOut = new FileOutputStream(file);wb.write(fileOut);fileOut.close();} catch (Exception e) {e.printStackTrace();}}}?
轉(zhuǎn)載于:https://www.cnblogs.com/zj0208/p/7737922.html
總結(jié)
以上是生活随笔為你收集整理的通过poi的XSSF实现生成excel文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios学习——键盘的收起
- 下一篇: Android中三种常用解析XML的方式