(四)JAVA使用POI操作excel

1,字体处理

 Demo12.java

 1 package com.wishwzp.poi;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 6 import org.apache.poi.ss.usermodel.Cell;
 7 import org.apache.poi.ss.usermodel.CellStyle;
 8 import org.apache.poi.ss.usermodel.Font;
 9 import org.apache.poi.ss.usermodel.Row;
10 import org.apache.poi.ss.usermodel.Sheet;
11 import org.apache.poi.ss.usermodel.Workbook;
12 
13 public class Demo12 {
14 
15     public static void main(String[] args) throws Exception{
16         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
17         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
18         Row row=sheet.createRow(1); // 创建一个行
19         
20         // 创建一个字体处理类
21         Font font=wb.createFont();
22         font.setFontHeightInPoints((short)24);//设置字字体高度
23         font.setFontName("Courier New");//设置字体
24         font.setItalic(true);//设置斜体
25         font.setStrikeout(true);//设置删除线
26         
27         CellStyle style=wb.createCellStyle();//创建样式
28         style.setFont(font);//将字体设置到样式
29         
30         Cell cell=row.createCell((short)1);
31         cell.setCellValue("This is test of fonts");
32         cell.setCellStyle(style);
33         
34         FileOutputStream fileOut=new FileOutputStream("d:\工作簿.xls");
35         wb.write(fileOut);
36         fileOut.close();
37     }
38 }

 

2,读取和重写工作簿

  Demo13.java

 1 package com.wishwzp.poi;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.InputStream;
 6 
 7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 8 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 9 import org.apache.poi.ss.usermodel.Cell;
10 import org.apache.poi.ss.usermodel.Row;
11 import org.apache.poi.ss.usermodel.Sheet;
12 import org.apache.poi.ss.usermodel.Workbook;
13 
14 public class Demo13 {
15 
16     public static void main(String[] args) throws Exception{
17         InputStream inp=new FileInputStream("d:\工作簿.xls");
18         POIFSFileSystem fs=new POIFSFileSystem(inp);
19         Workbook wb=new HSSFWorkbook(fs);
20         Sheet sheet=wb.getSheetAt(0);  // 获取第一个Sheet页
21         Row row=sheet.getRow(0); // 获取第一行
22         Cell cell=row.getCell(0); // 获取单元格
23         if(cell==null){
24             cell=row.createCell(3);
25         }
26         cell.setCellType(Cell.CELL_TYPE_STRING);
27         cell.setCellValue("测试单元格");
28         
29         FileOutputStream fileOut=new FileOutputStream("d:\工作簿1.xls");
30         wb.write(fileOut);
31         fileOut.close();
32     }
33 }

 我们上面的例子上读取了工作薄.xls,将读取的数据又创建了工作薄1.xls。

 

3,单元格中使用换行

  Demo14.java

 1 package com.wishwzp.poi;
 2 
 3 import java.io.FileOutputStream;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 
 7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 8 import org.apache.poi.ss.usermodel.Cell;
 9 import org.apache.poi.ss.usermodel.CellStyle;
10 import org.apache.poi.ss.usermodel.CreationHelper;
11 import org.apache.poi.ss.usermodel.IndexedColors;
12 import org.apache.poi.ss.usermodel.Row;
13 import org.apache.poi.ss.usermodel.Sheet;
14 import org.apache.poi.ss.usermodel.Workbook;
15 import org.apache.poi.ss.util.CellRangeAddress;
16 
17 public class Demo14 {
18 
19     public static void main(String[] args) throws Exception{
20         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
21         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
22         Row row=sheet.createRow(2); // 创建一个行
23         Cell cell=row.createCell(2);
24         cell.setCellValue("我要换行 
 成功了吗?");
25         
26         CellStyle cs=wb.createCellStyle();
27         // 设置可以换行
28         cs.setWrapText(true);
29         cell.setCellStyle(cs);
30         
31         // 调整下行的高度
32         row.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints());
33         // 调整单元格宽度
34         sheet.autoSizeColumn(2);
35         
36         FileOutputStream fileOut=new FileOutputStream("d:\工作簿.xls");
37         wb.write(fileOut);
38         fileOut.close();
39     }
40 }

 

 


4,创建用户自定义数据格式

  Demo15.java

 1 package com.wishwzp.poi;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 6 import org.apache.poi.ss.usermodel.Cell;
 7 import org.apache.poi.ss.usermodel.CellStyle;
 8 import org.apache.poi.ss.usermodel.DataFormat;
 9 import org.apache.poi.ss.usermodel.Row;
10 import org.apache.poi.ss.usermodel.Sheet;
11 import org.apache.poi.ss.usermodel.Workbook;
12 
13 public class Demo15 {
14 
15     public static void main(String[] args) throws Exception{
16         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
17         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
18         CellStyle style;
19         DataFormat format=wb.createDataFormat();
20         Row row;
21         Cell cell;
22         short rowNum=0;
23         short colNum=0;
24         
25         row=sheet.createRow(rowNum++);
26         cell=row.createCell(colNum);
27         cell.setCellValue(111111.25);
28         
29         style=wb.createCellStyle();
30         style.setDataFormat(format.getFormat("0.0")); // 设置数据格式
31         cell.setCellStyle(style);
32         
33         row=sheet.createRow(rowNum++);
34         cell=row.createCell(colNum);
35         cell.setCellValue(1111111.25);
36         style=wb.createCellStyle();
37         style.setDataFormat(format.getFormat("#,##0.000"));
38         cell.setCellStyle(style);
39         
40         FileOutputStream fileOut=new FileOutputStream("d:\工作簿.xls");
41         wb.write(fileOut);
42         fileOut.close();
43     }
44 }

原文地址:https://www.cnblogs.com/wishwzp/p/5495076.html