Java利用POI生成Excel強制換行


Java利用POI生成Excel強制換行

使用POI創建一個簡單的   myXls.xls   文件  
   
常用的包為   org.apache.poi.hssf.usermodel.*;  
例子:  
   
import   java.io.*;  
import   org.apache.poi.hssf.usermodel.*;  
   
public   class   ZoomSheet   {  
   
          public   ZoomSheet()   {  
          }  
   
          public   static   void   main(String   args[])  
                  throws   IOException   {  
   
                  HSSFWorkbook   wb   =   new   HSSFWorkbook();  
                  HSSFSheet   sheet1   =   wb.createSheet("new   sheet");  
                  FileOutputStream   fileOut   =   new   FileOutputStream("workbook.xls");  
                  wb.write(fileOut);  
                  fileOut.close();  
          }  
}  
   
類:  
HSSFWorkbook 創建   xls   的對象; HSSFWorkbook   hw   =   new   HSSFWorkbook();                  
設置分區顯示; hw.setRepeatingRowsAndColumns(sheet的index,   行,   列,   行,   列);  
HSSFSheet 創建   xls   中的sheet(工作表); HSSFSheet   sheet   =   hw.createSheet("sheet1");       sheet1   是   sheet   的名稱   可缺省  
設置列高; sheet.setColumnWidth((short)short,   (short)short);  
HSSFRow 創建   xls   中的行; HSSFRow   row   =   sheet.createRow(0);                             0   表示第一行  
設置行高; row.setHeight((short)short);  
HSSFFont 創建   xls   中的字體; HSSFFont   font   =   hw.createFont();  
設定字體大小; font.setFontHeightInPoints((short)54);  
設定為斜體; font.setItalic(true);  
設定文字刪除線; font.setStrikeout(true);  
HSSFCellStyle 設定單元格風格; HSSFCellStyle   style   =   wb.createCellStyle();  
加入字體; style.setFont(font);  
HSSFCell 設定單元格; HSSFCell   cell   =   row.createCell((short)0);  
單元格水平對齊方式; style.setAlignment(align);         //單元格水平   0   普通   1   左對齊   2   居中   3   右對齊   4   填充   5   正當   6   居中選擇  
單元格垂直對齊方式; style.setVerticalAlignment(align);       //單元格垂直   0   居上   1   居中   2   居下   3   正當  
   
單元格下邊框為細線; style.setBorderBottom((short)short);  
同上一命令一同使用,設置顏色; style.setBottomBorderColor((short)short);  
單元格左邊框; style.setBorderLeft((short)short);  
style.setLeftBorderColor((short)short);  
單元格右邊框; style.setBorderRight((short)short);  
style.setRightBorderColor((short)short);  
單元格上邊框; style.setBorderTop((short)short);  
style.setTopBorderColor((short)short);  
單元格字符編號(中文); cell.setEncoding(HSSFCell.ENCODING_UTF_16);       //中文  
單元格顯示的值; cell.setCellValue("中醫葯");     值的類型有:double,int,String,Date,boolean  
單元格背景色; style.setFillForegroundColor((short)short);  
圖案類型; style.setFillPattern((short)short);  
單元格合並; sheet.addMergedRegion(new   Region(行,   (short)列,   行,   (short)列));  
   
   
單元格風格加入; cell.setCellStyle(style);  
   
               
   
   
打印設置  
引入包 import   org.apache.poi.hssf.usermodel.HSSFPrintSetup;  
創建打印設置對象 HSSFPrintSetup   hps   =   hs.getPrintSetup();  
設置A4紙 hps.setPaperSize((short)9);      
將頁面設置為橫向打印模式 hps.setLandscape(true);    
   
設置打印頁面為水平居中 sheet.setHorizontallyCenter(true);        
設置打印頁面為垂直居中 sheet.setVerticallyCenter(true);

 

網上找到的文章都是說在excel里的文字里加上\n,\n\r,\r\n之類,反正各種各樣的都有,更奇怪的是還有人說在單元格里加上<br>
后來我試過用\r后的效里是生成的文件里,你用打開時,並不會換行,如果你用鼠標在單元格里點一下之后就會自動換行。


可以通過如下方式進行,
1. 首先在需要強制換行的單元格里使用poi的樣式,並且把樣式設定為自動換行
   # HSSFCellStyle cellStyle=workbook.createCellStyle();    
   # cellStyle.setWrapText(true);    
   # cell.setCellStyle(cellStyle);
2. 其次是在需要強制換行的單元格,使用\就可以實再強制換行
   1. HSSFCell cell = row.createCell((short)0);
   2. cell.setCellStyle(cellStyle);                           cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));
這樣就能實現強制換行,
換行后的效里是單元格里強制換行
hello
world!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM