POI设置excle单元格样式

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里的文字里加上 , , 之类,反正各种各样的都有,更奇怪的是还有人说在单元格里加上<br> 
后来我试过用 后的效里是生成的文件里,你用打开时,并不会换行,如果你用鼠标在单元格里点一下之后就会自动换行。


可以通过如下方式进行,
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 world!")); 
这样就能实现强制换行,
换行后的效里是单元格里强制换行
hello
world!

 
 

public class ExcelTest {
  /**   
  * @param args   
  */   
  public static void main(String[] args) throws IOException {    
 
      try {    
          HSSFWorkbook wb = new HSSFWorkbook();    
          HSSFSheet sheet = wb.createSheet("new   sheet");    
          HSSFCellStyle style = wb.createCellStyle(); // 样式对象    
 
          style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
          style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平    
          /**字体begin*/
          style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); 
          //背景颜色
//          style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//          style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//          style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//          style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//          style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//          style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          
          //生成一个字体
          HSSFFont font=wb.createFont();
          font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色
          font.setFontHeightInPoints((short)12);
          font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);         //字体增粗
         //把字体应用到当前的样式
          style.setFont(font);
          /**字体end*/
          HSSFRow row = sheet.createRow((short) 0);    
          HSSFRow row2 = sheet.createRow((short) 1);    
          
          // 四个参数分别是:起始行,起始列,结束行,结束列    
          sheet.addMergedRegion(new Region(0, (short) 0, 5, (short) 0));    
          HSSFCell ce = row.createCell((short) 0);    
          ce.setCellValue("项目\日期"); // 表格的第一行第一列显示的数据    
          ce.setCellStyle(style); // 样式,居中    
          int num = 0;    
          for (int i = 0; i < 9; i++) { // 循环9次,每一次都要跨单元格显示    
              // 计算从那个单元格跨到那一格    
              int celln = 0;    
              int celle = 0;    
              if (i == 0) {    
                  celln = 0;    
                  celle = 1;    
              } else {    
                  celln = (i * 2);    
                  celle = (i * 2 + 1);    
              }    
              // 单元格合并    
              // 四个参数分别是:起始行,起始列,结束行,结束列    
              sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,    
                      (short) (celle + 1)));    
              HSSFCell cell = row.createCell((short) (celln + 1));    
              cell.setCellValue("merging" + i); // 跨单元格显示的数据    
              cell.setCellStyle(style); // 样式    
              // 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”    
              HSSFCell cell1 = row2.createCell((short) celle);    
              HSSFCell cell2 = row2.createCell((short) (celle + 1));    
              cell1.setCellValue("数量");    
              cell1.setCellStyle(style);    
              cell2.setCellValue("金额");    
              cell2.setCellStyle(style);    
              num++;    
          }    
 
          // 在后面加上合计百分比    
 
          // 合计 在最后加上,还要跨一个单元格     //四个参数分别是:起始行,起始列,结束行,结束列    
          sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,    
                  (short) (2 * num + 2)));    
          HSSFCell cell = row.createCell((short) (2 * num + 1));    
          cell.setCellValue("合计");    
          cell.setCellStyle(style);  
          HSSFCell cell1 = row2.createCell((short) (2 * num + 1));    
          HSSFCell cell2 = row2.createCell((short) (2 * num + 2));    
          cell1.setCellValue("数量");    
          cell1.setCellStyle(style);    
          cell2.setCellValue("金额");    
          cell2.setCellStyle(style);    
 
          // 百分比 同上    
          sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,    
                  (short) (2 * num + 4)));    
          HSSFCell cellb = row.createCell((short) (2 * num + 3));    
          cellb.setCellValue("百分比");    
          cellb.setCellStyle(style);    
          HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));    
          HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));    
          cellb1.setCellValue("数量");    
          cellb1.setCellStyle(style);    
          cellb2.setCellValue("金额");    
          cellb2.setCellStyle(style);  
          
          //输出一些数据 然后再输出表头
          
          FileOutputStream fileOut = new FileOutputStream("D://workbook.xls");   
          wb.write(fileOut);    
          fileOut.close();    
          System.out.print("OK");    
      } catch (Exception ex) {    
          ex.printStackTrace();    
      }    
 
  }    
 

//设置单元格字体颜色

import Java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.CreationHelper;

import org.apache.poi.ss.usermodel.Font;

import org.apache.poi.ss.usermodel.RichTextString;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

public class test {

public static void main(String[] args) {

Workbook workbook = new HSSFWorkbook();

Sheet sheet = workbook.createSheet();

Cell cell = sheet.createRow(0).createCell(0);

CreationHelper helper = workbook.getCreationHelper();

RichTextString str = helper.createRichTextString("a b c d e ");// 在这里使用 表示回车

Font[] fonts = new Font[5];

fonts[0] = workbook.createFont();

fonts[0].setColor(HSSFColor.YELLOW.index);

fonts[1] = workbook.createFont();

fonts[1].setColor(HSSFColor.RED.index);

fonts[2] = workbook.createFont();

fonts[2].setColor(HSSFColor.BLUE.index);

fonts[3] = workbook.createFont();

fonts[3].setColor(HSSFColor.ROSE.index);

fonts[4] = workbook.createFont();

fonts[4].setColor(HSSFColor.BLACK.index);

for (int i = 0; i < 5; i++) {

str.applyFont(i * 2, (i + 1) * 2, fonts[i]);

}

cell.setCellValue(str);

try {

FileOutputStream out = new FileOutputStream(new File("d:\1.xls"));

workbook.write(out);

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

原文地址:https://www.cnblogs.com/langtianya/p/6434843.html