使用poi进行数据的导出Demo
生活随笔
收集整理的這篇文章主要介紹了
使用poi进行数据的导出Demo
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這是本人在項(xiàng)目中遇到了一個(gè)導(dǎo)出數(shù)據(jù)時(shí),如果該條數(shù)據(jù)中包含漢字,就會(huì)出現(xiàn)excel單元格的大小與期望的樣式不一樣,也是查找了半天,也沒有發(fā)現(xiàn)哪里出的問題.
現(xiàn)將一個(gè)小Demo奉獻(xiàn)在這里,可以在遇到使用poi導(dǎo)出數(shù)據(jù)時(shí),可以用來(lái)進(jìn)行參考.
使用的是Springboot+poi
引入的依賴就只有poi的依賴
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.16</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.16</version></dependency>編寫的測(cè)試類為:
package exercise.demo;import org.apache.poi.hssf.usermodel.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import java.io.FileOutputStream;@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests {@Testpublic void excelUtils() {/*** @see <a href="http://poi.apache.org/hssf/quick-guide.html#NewWorkbook">For more</a>*/// 創(chuàng)建新的Excel 工作簿HSSFWorkbook workbook = new HSSFWorkbook();// 在Excel工作簿中建一工作表,其名為缺省值, 也可以指定Sheet名稱HSSFSheet sheet = workbook.createSheet();//HSSFSheet sheet = workbook.createSheet("SheetName");// 用于格式化單元格的數(shù)據(jù)HSSFDataFormat format = workbook.createDataFormat();// 創(chuàng)建新行(row),并將單元格(cell)放入其中. 行號(hào)從0開始計(jì)算.HSSFRow row = sheet.createRow((short) 1);// 設(shè)置字體HSSFFont font = workbook.createFont();font.setFontHeightInPoints((short) 20); //字體高度font.setColor(HSSFFont.COLOR_RED); //字體顏色font.setFontName("黑體"); //字體font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //寬度font.setItalic(true); //是否使用斜體 // font.setStrikeout(true); //是否使用劃線// 設(shè)置單元格類型HSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setFont(font);cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中cellStyle.setWrapText(true);// 添加單元格注釋// 創(chuàng)建HSSFPatriarch對(duì)象,HSSFPatriarch是所有注釋的容器.HSSFPatriarch patr = sheet.createDrawingPatriarch();// 定義注釋的大小和位置,詳見文檔HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));// 設(shè)置注釋內(nèi)容comment.setString(new HSSFRichTextString("可以在POI中添加注釋!"));// 設(shè)置注釋作者. 當(dāng)鼠標(biāo)移動(dòng)到單元格上是可以在狀態(tài)欄中看到該內(nèi)容.comment.setAuthor("Xuys.");// // 創(chuàng)建單元格HSSFCell cell = row.createCell((short) 1); // HSSFRichTextString hssfString = new HSSFRichTextString("Hello World!"); // cell.setCellValue(hssfString);//設(shè)置單元格內(nèi)容 // cell.setCellStyle(cellStyle);//設(shè)置單元格樣式 // cell.setCellType(HSSFCell.CELL_TYPE_STRING);//指定單元格格式:數(shù)值、公式或字符串 // cell.setCellComment(comment);//添加注釋 // // //格式化數(shù)據(jù) // row = sheet.createRow((short) 2); // cell = row.createCell((short) 2); // cell.setCellValue(11111.25); // cellStyle = workbook.createCellStyle(); // cellStyle.setDataFormat(format.getFormat("0.0")); // cell.setCellStyle(cellStyle); // // row = sheet.createRow((short) 3); // cell = row.createCell((short) 3); // cell.setCellValue(9736279.073); // cellStyle = workbook.createCellStyle(); // cellStyle.setDataFormat(format.getFormat("#,##0.0000")); // cell.setCellStyle(cellStyle);// sheet.autoSizeColumn((short)1); //調(diào)整第一列寬度 // sheet.autoSizeColumn((short)1); //調(diào)整第二列寬度 // sheet.autoSizeColumn((short)2); //調(diào)整第三列寬度 // sheet.autoSizeColumn((short)3); //調(diào)整第四列寬度try {FileOutputStream fileOut = new FileOutputStream("G:/3.xls");workbook.write(fileOut);fileOut.close();} catch (Exception e) {System.out.println(e.toString());}}}直接放在測(cè)試類中運(yùn)行這段代碼就會(huì)在指定的文件中出現(xiàn)這個(gè)Excel文件.
轉(zhuǎn)載于:https://www.cnblogs.com/qingmuchuanqi48/p/11286062.html
總結(jié)
以上是生活随笔為你收集整理的使用poi进行数据的导出Demo的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决虚拟机vmware安装64位系统“此
- 下一篇: 转载:SqlServer数据库性能优化详