簡單總結poi的使用方法

使用的jar包: poi-3.17.jar poi-examples-3.17.jar poi-excelant-3.17.jar poi-ooxml-3.17.jar poi-ooxml-schemas-3.17.jar poi-scratchpad-3.17.jar 對自己使用poi的一個簡單的總結,備忘一下供自己以後參考,也希望對剛接觸poi的朋友有所幫助,其他一些樣式的使用方法請自行上網搜索。 // 創建HSSFWorkbook對象(excel的文檔對象) HSSFWorkbook workbook = new HSSFWorkbook(); //設置字體樣式 HSSFFont font = workbook.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 14);// 設置字體大小 // 建立新的sheet對象(excel的表單) HSSFSheet sheet = workbook.createSheet(); //定義行樣式 HSSFCellStyle cell_Style = (HSSFCellStyle) workbook.createCellStyle(); cell_Style.setBorderBottom(BorderStyle.THIN); // 下邊框 cell_Style.setBorderLeft(BorderStyle.THIN);// 左邊框 cell_Style.setBorderTop(BorderStyle.THIN);// 上邊框 cell_Style.setBorderRight(BorderStyle.THIN);// 右邊框 cell_Style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直對齊居中 //字體樣式定義完成後要放入行樣式中才能生效 cell_Style.setFont(font); //創建第一行 HSSFRow row = sheet.createRow((short) 0); // 表頭第一行合併5列 參數(第一行,第一行,第一列,第五列) CellRangeAddress region = new CellRangeAddress(0, 0, 0, 4); sheet.addMergedRegion(region); HSSFCell cell = null; for (int i = 0; i < title.size(); i++) { //循環創建單元格 cell = row.createCell(i); //設置精準行高 參數(想設置的行高*20)=Excel中的行高 row.setHeight((short)(43.5*20)); //爲單元格附加樣式 cell.setCellStyle(cell_Style); //將值傳入單元格中 cell.setCellValue(title.get(i)); } //循環的數據內容從第幾行數開始 //表頭部佔了4行 int k=3; //定義序號列的值 int j=1; // str我是用String數組來存儲數據的 for (int i = 0; i < str.length; i=i++) { // 創建循環數據的第一行 HSSFRow nextRow = sheet.createRow(k); // 創建第一列 根據需要來創建多少列 HSSFCell cell0 = nextRow.createCell(0); //賦予第一列樣式 cell0.setCellStyle(cell_Style); // 賦值 序號 cell0.setCellValue(j); } //精準設置列寬 參數(第幾列,(int)((要設置的列寬 + 0.72) * 256))=Excel列寬 sheet.setColumnWidth(0, (int)((8.38 + 0.72) * 256)); //列寬的設置寫在最後 sheet.autoSizeColumn(i);//自動調整列寬
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章