Excel的匯入和匯出功能實現
阿新 • • 發佈:2019-01-26
工作中經常會用到excel的匯入和匯出功能,這裡我提供匯入和匯出類。
匯入類(需要注意的地方我註釋裡面寫好了):
package cn.teacheredu.utils; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * 被解析的Excel最好是什麼樣的呢? * 單元格最好都是文字格式,儲存資料前自己去轉換,不用poi帶的轉換。 * 第一列 和最後一列 必須是必填欄位!!!這樣的你用我這個Util,得到的List就很準確了,不會出現多餘的行或列。 * @author TMACJ * @version 0.000000.002899 */ public class ImportExcelUtil { private final static String excel2003L =".xls"; //2003- 版本的excel private final static String excel2007U =".xlsx"; //2007+ 版本的excel static SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); static short[] yyyyMMdd = {14, 31, 57, 58, 179, 184, 185, 186, 187, 188}; static short[] HHmmss = {20, 32, 190, 191, 192}; static List<short[]> yyyyMMddList = Arrays.asList(yyyyMMdd); static List<short[]> hhMMssList = Arrays.asList(HHmmss); /** * 描述:獲取IO流中的資料,組裝成List<List<Object>>物件 * @param in,fileName * @return * @throws IOException */ public List<List<String>> getBankListByExcel(InputStream in,String fileName) throws Exception{ List<List<String>> list = null; //建立Excel工作薄 Workbook work = this.getWorkbook(in,fileName); if(null == work){ throw new Exception("建立Excel工作薄為空!"); } Sheet sheet = null; Row row = null; Cell cell = null; list = new ArrayList<List<String>>(); //遍歷Excel中所有的sheet for (int i = 0; i < work.getNumberOfSheets(); i++) { sheet = work.getSheetAt(i); if(sheet==null){continue;} int totalCell = sheet.getRow(0).getPhysicalNumberOfCells();//標題行一共有多少列 //遍歷當前sheet中的所有行 for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum()+1; j++) { row = sheet.getRow(j); if(row==null || validateRow(row) || row.getPhysicalNumberOfCells() < totalCell){continue;} //3個條件,有一個為true就不會往list里加,不僅過濾空行還過濾了列數不夠的行,這點要注意,要求表中前後的列都是必填的。 //遍歷所有的列 List<String> li = new ArrayList<String>(); for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { cell = row.getCell(y); li.add(this.getCellData(cell)); } list.add(li); } // 簡單起見,這裡只解析第一個工作簿! break; } work.close(); return list; } // 過濾空行,(其中一行的資料的確都為空,可是其原本的格式還在,並沒有連帶刪除,這樣計算出來的行數就不真實,比真實的大) private boolean validateRow(Row row) throws Exception{ // for (Cell cell : row) { // // } //只判斷第一列。第一列為空就代表這行的資料無效 if (row.getCell(0).getCellType() == Cell.CELL_TYPE_BLANK || "".equals(this.getCellData(row.getCell(0)))) { return true; } return false;//不是空行 } /** * 描述:根據檔案字尾,自適應上傳檔案的版本 * @param inStr,fileName * @return * @throws Exception */ public Workbook getWorkbook(InputStream inStr,String fileType) throws Exception{ Workbook wb = null; if(excel2003L.equals(fileType)){ wb = new HSSFWorkbook(inStr); //2003- }else if(excel2007U.equals(fileType)){ wb = new XSSFWorkbook(inStr); //2007+ }else{ throw new Exception("解析的檔案格式有誤!"); } return wb; } /** * 獲取單元中值(字串型別) * * @param cell * @return * @throws Exception */ public String getCellData(Cell cell) throws Exception { String cellValue = ""; if (cell != null) { try { switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK://空白 cellValue = ""; break; case Cell.CELL_TYPE_NUMERIC: //數值型 0----日期型別也是數值型的一種 if (DateUtil.isCellDateFormatted(cell)) { short format = cell.getCellStyle().getDataFormat(); if (yyyyMMddList.contains(format)) { sFormat = new SimpleDateFormat("yyyy-MM-dd"); } else if (hhMMssList.contains(format)) { sFormat = new SimpleDateFormat("HH:mm:ss"); } Date date = cell.getDateCellValue(); cellValue = sFormat.format(date); } else { cell.setCellType(Cell.CELL_TYPE_STRING); cellValue = replaceBlank(cell.getStringCellValue()); //Double numberDate = new BigDecimal(cell.getNumericCellValue()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();//似乎還是有點問題 //cellValue = numberDate + ""; } break; case Cell.CELL_TYPE_STRING: //字串型 1 cellValue = replaceBlank(cell.getStringCellValue()); break; case Cell.CELL_TYPE_FORMULA: //公式型 2 cell.setCellType(Cell.CELL_TYPE_STRING); cellValue = replaceBlank(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: //布林型 4 cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: //錯誤 5 cellValue = "!#REF!"; break; } } catch (Exception e) { throw new Exception("讀取Excel單元格資料出錯:" + e.getMessage()); } } return cellValue; } public static String replaceBlank(String source) { String dest = ""; if (source != null) { Pattern p = Pattern.compile("\t|\r|\n"); Matcher m = p.matcher(source); dest = m.replaceAll(""); } return dest.trim(); } }
匯出類(.XLS格式):
package cn.teacheredu.utils; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.util.CellRangeAddress; /** * 通用的匯出Excel類,如果需要自定義格式的,參照此類自己再寫類或方法來實現 * dataList裡的每一個Object陣列一個元素(object[0])都是序號,不可放真實資料 * @authorTMACJ */ public class ExportExcelUtil { private String title; // 匯出表格的表名 private String[] rowName;// 匯出表格的列名 private List<Object[]> dataList = new ArrayList<Object[]>(); // 物件陣列的List集合 private HttpServletResponse response; private HttpServletRequest request; /** * 例項化匯出類 * @param title 匯出表格的表名,最好是英文,中文可能出現亂碼 * @param rowName 匯出表格的列名陣列 * @param dataList 物件陣列的List集合 * @param response */ public ExportExcelUtil(String title,String[] rowName,List<Object[]> dataList, HttpServletRequest request, HttpServletResponse response){ this.title=title; this.rowName=rowName; this.dataList=dataList; this.response = response; this.request = request; } // 匯出資料 public void exportData() throws Exception{ HSSFWorkbook workbook =new HSSFWorkbook(); // 建立一個excel物件 HSSFSheet sheet =workbook.createSheet(title); // 建立表格 //sheet.setDefaultRowHeightInPoints(18.5f); // sheet樣式定義 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook,16); // 頭樣式 HSSFCellStyle columnStyle = this.getColumnStyle(workbook,14); // 標題樣式 HSSFCellStyle style = this.getStyle(workbook,11); // 單元格樣式 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (rowName.length-1)));// 合併第一行的所有列 // 產生表格標題行 HSSFRow rowm =sheet.createRow(0); // 行 rowm.setHeightInPoints(26f); HSSFCell cellTiltle =rowm.createCell(0); // 單元格 cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); int columnNum = rowName.length; // 表格列的長度 HSSFRow rowRowName = sheet.createRow(1); // 在第二行建立行 HSSFCellStyle cells =workbook.createCellStyle(); cells.setBottomBorderColor(HSSFColor.BLACK.index); rowRowName.setRowStyle(cells); // 迴圈 將列名放進去 for (int i = 0; i < columnNum; i++) { HSSFCell cellRowName = rowRowName.createCell(i); cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 單元格型別 HSSFRichTextString text = new HSSFRichTextString(rowName[i]); // 得到列的值 cellRowName.setCellValue(text); // 設定列的值 cellRowName.setCellStyle(columnStyle); // 樣式 } // 將查詢到的資料設定到對應的單元格中 for (int i = 0; i < dataList.size(); i++) { Object[] obj = dataList.get(i);//遍歷每個物件 HSSFRow row = sheet.createRow(i+2);//建立所需的行數 for (int j = 0; j < obj.length; j++) { HSSFCell cell = null; //設定單元格的資料型別 if(j==0){ // 第一列設定為序號 cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //設定單元格的值 }else{ cell.setCellValue(" "); } } cell.setCellStyle(style); // 樣式 } } // 讓列寬隨著匯出的列長自動適應,但是對中文支援不是很好 也可能在linux(無圖形環境的作業系統)下報錯,報錯再說 for (int i = 0; i < columnNum; i++) { sheet.autoSizeColumn(i); sheet.setColumnWidth(i, sheet.getColumnWidth(i)+888);//適當再寬點 } if(workbook !=null){ // 輸出到伺服器上 // FileOutputStream fileOutputStream = new FileOutputStream("D:/user.xls"); // workbook.write(fileOutputStream);//將資料寫出去 // fileOutputStream.close();//關閉輸出流 // 輸出到使用者瀏覽器上 OutputStream out = response.getOutputStream(); try { // excel 表文件名 String fileName = title + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ".xls"; String fileName11 = ""; String userAgent = request.getHeader("USER-AGENT"); if(StringUtils.contains(userAgent, "Firefox") || StringUtils.contains(userAgent, "firefox")){//火狐瀏覽器 fileName11 = new String(fileName.getBytes(), "ISO8859-1"); }else{ fileName11 = URLEncoder.encode(fileName,"UTF-8");//其他瀏覽器 } String headStr = "attachment; filename=\"" + fileName11 + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", headStr); workbook.write(out); out.flush(); workbook.close(); } catch (Exception e) { throw e; } finally { if (null != out) { out.close(); } } } } public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook,int fontSize) { // 設定字型 HSSFFont font = workbook.createFont(); //設定字型大小 font.setFontHeightInPoints((short)fontSize); //字型加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("宋體"); //設定樣式; HSSFCellStyle style = workbook.createCellStyle(); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } public HSSFCellStyle getColumnStyle(HSSFWorkbook workbook,int fontSize) { // 設定字型 HSSFFont font = workbook.createFont(); //設定字型大小 font.setFontHeightInPoints((short)fontSize); //字型加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("宋體"); //設定樣式; HSSFCellStyle style = workbook.createCellStyle(); //設定底邊框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設定底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設定左邊框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //設定左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設定右邊框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //設定右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設定頂邊框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //設定頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } public HSSFCellStyle getStyle(HSSFWorkbook workbook,int fontSize) { //設定字型 HSSFFont font = workbook.createFont(); //設定字型大小 font.setFontHeightInPoints((short)fontSize); //字型加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("宋體"); //設定樣式; HSSFCellStyle style = workbook.createCellStyle(); //設定底邊框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設定底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設定左邊框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //設定左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設定右邊框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //設定右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設定頂邊框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //設定頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }
匯出類(.xlsx格式):
package cn.teacheredu.utils; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; 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.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFRichTextString; /** * 通用的匯出Excel類,(Excel 2007 OOXML (.xlsx)格式 )如果需要自定義格式的,參照此類自己再寫類或方法來實現 * dataList裡的每一個Object陣列一個元素(object[0])都是序號,不可放真實資料 * @author Zhaojie */ public class ExportExcelUtil2 { private String title; // 匯出表格的表名 private String[] rowName;// 匯出表格的列名 private List<Object[]> dataList = new ArrayList<Object[]>(); // 物件陣列的List集合 private HttpServletResponse response; private HttpServletRequest request; /** * 例項化匯出類 * @param title 匯出表格的表名,最好是英文,中文可能出現亂碼 * @param rowName 匯出表格的列名陣列 * @param dataList 物件陣列的List集合 * @param response */ public ExportExcelUtil2(String title,String[] rowName,List<Object[]> dataList, HttpServletRequest request, HttpServletResponse response){ this.title=title; this.rowName=rowName; this.dataList=dataList; this.response = response; this.request = request; } // 匯出資料 public void exportData() throws Exception{ SXSSFWorkbook workbook = new SXSSFWorkbook();//宣告一個工作薄 Excel 2007 OOXML (.xlsx)格式 SXSSFSheet sheet = workbook.createSheet(title); // 建立表格 for(int i = 1;i<rowName.length;i++){ //根據列名設定每一列的寬度 int length = rowName[i].toString().length(); sheet.setColumnWidth(i, 2*(length+1)*256); } //sheet.setDefaultRowHeightInPoints(18.5f); // sheet樣式定義 CellStyle columnTopStyle = this.getColumnTopStyle(workbook,14); // 頭樣式 CellStyle columnStyle = this.getColumnStyle(workbook,12); // 標題樣式 CellStyle style = this.getStyle(workbook,11); // 單元格樣式 // 產生表格標題行 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (rowName.length-1)));// 合併第一行的所有列 SXSSFRow rowm = sheet.createRow(0); // 行 rowm.setHeightInPoints(31f); SXSSFCell cellTiltle = rowm.createCell(0); // 單元格 cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); // 產生第二行(列名) int columnNum = rowName.length; // 表格列的長度 SXSSFRow rowRowName = sheet.createRow(1); // 在第二行建立行 rowRowName.setHeightInPoints(21f); CellStyle cells = workbook.createCellStyle(); cells.setBottomBorderColor(HSSFColor.BLACK.index); rowRowName.setRowStyle(cells); for (int i = 0; i < columnNum; i++) { SXSSFCell cellRowName = rowRowName.createCell(i); cellRowName.setCellType(SXSSFCell.CELL_TYPE_STRING); // 單元格型別 XSSFRichTextString text = new XSSFRichTextString(rowName[i]); // 得到列的值 cellRowName.setCellValue(text); // 設定列的值 cellRowName.setCellStyle(columnStyle); // 樣式 } // 產生其它行(將資料列表設定到對應的單元格中)注意:預設添加了第一列的序號,如果不要可以註釋掉 for (int i = 0; i < dataList.size(); i++) { Object[] obj = dataList.get(i);//遍歷每個物件 SXSSFRow row = sheet.createRow(i+2);//建立所需的行數 row.setHeightInPoints(17.25f); for (int j = 0; j < obj.length; j++) { SXSSFCell cell = null; //設定單元格的資料型別 if(j==0){ // 第一列設定為序號 cell = row.createCell(j,SXSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,SXSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //設定單元格的值 }else{ cell.setCellValue(" "); } } cell.setCellStyle(style); // 樣式 } } // 讓列寬隨著匯出的列長自動適應,但是對中文支援不是很好 也可能在linux(無圖形環境的作業系統)下報錯,報錯再說 // for (int i = 0; i < columnNum; i++) { // sheet.autoSizeColumn(i); // sheet.setColumnWidth(i, sheet.getColumnWidth(i)+888);//適當再寬點 // } if(workbook !=null){ // 輸出到伺服器上 // FileOutputStream fileOutputStream = new FileOutputStream("D:/user.xls"); // workbook.write(fileOutputStream);//將資料寫出去 // fileOutputStream.close();//關閉輸出流 // 輸出到使用者瀏覽器上 OutputStream out = response.getOutputStream(); try { // excel 表文件名 String fileName = title + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ".xlsx"; String fileName11 = ""; String userAgent = request.getHeader("USER-AGENT"); if(StringUtils.contains(userAgent, "Firefox") || StringUtils.contains(userAgent, "firefox")){//火狐瀏覽器 fileName11 = new String(fileName.getBytes(), "ISO8859-1"); }else{ fileName11 = URLEncoder.encode(fileName,"UTF-8");//其他瀏覽器 } String headStr = "attachment; filename=\"" + fileName11 + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", headStr); workbook.write(out); out.flush(); workbook.close(); workbook.dispose(); } catch (Exception e) { throw e; } finally { if (null != out) { out.close(); } } } } public CellStyle getColumnTopStyle(SXSSFWorkbook workbook,int fontSize) { // 設定字型 Font font = workbook.createFont(); //設定字型大小 font.setFontHeightInPoints((short)fontSize); //字型加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("宋體"); //設定樣式; CellStyle style = workbook.createCellStyle(); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(CellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); return style; } public CellStyle getColumnStyle(SXSSFWorkbook workbook,int fontSize) { // 設定字型 Font font = workbook.createFont(); //設定字型大小 font.setFontHeightInPoints((short)fontSize); //字型加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("宋體"); //設定樣式; CellStyle style = workbook.createCellStyle(); //設定底邊框; style.setBorderBottom(CellStyle.BORDER_THIN); //設定底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設定左邊框; style.setBorderLeft(CellStyle.BORDER_THIN); //設定左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設定右邊框; style.setBorderRight(CellStyle.BORDER_THIN); //設定右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設定頂邊框; style.setBorderTop(CellStyle.BORDER_THIN); //設定頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(CellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //設定背景填充色(前景色) style.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);//設定別的顏色請去網上查詢相關文件 style.setFillPattern(CellStyle.SOLID_FOREGROUND); return style; } public CellStyle getStyle(SXSSFWorkbook workbook,int fontSize) { //設定字型 Font font = workbook.createFont(); //設定字型大小 font.setFontHeightInPoints((short)fontSize); //字型加粗 //font.setBoldweight(Font.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("宋體"); //設定樣式; CellStyle style = workbook.createCellStyle(); //設定底邊框; style.setBorderBottom(CellStyle.BORDER_THIN); //設定底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設定左邊框; style.setBorderLeft(CellStyle.BORDER_THIN); //設定左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設定右邊框; style.setBorderRight(CellStyle.BORDER_THIN); //設定右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設定頂邊框; style.setBorderTop(CellStyle.BORDER_THIN); //設定頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(CellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); return style; } }
以上就是兩個工具類,如果你覺得本文對你有幫助,請點選一下關注或者頂一下,謝謝各位觀眾姥爺啦~