java 读取 excel

Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。

Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)。

  • HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
  • XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
  • HWPF - 提供读写Microsoft Word DOC格式档案的功能。
  • HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
  • HDGF - 提供读Microsoft Visio格式档案的功能。
  • HPBF - 提供读Microsoft Publisher格式档案的功能。
  • HSMF - 提供读Microsoft Outlook格式档案的功能。


http://poi.apache.org/

版本:poi-3.8-20120326.jar

1、生成Workbook

Java代码 复制代码 收藏代码
  1. //生成Workbook
  2. HSSFWorkbook wb = new HSSFWorkbook();
  3. //添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
  4. @SuppressWarnings("unused")
  5. Sheet sheet1 = wb.createSheet();
  6. @SuppressWarnings("unused")
  7. Sheet sheet2 = wb.createSheet();
  8. @SuppressWarnings("unused")
  9. Sheet sheet3 = wb.createSheet("new sheet");
  10. @SuppressWarnings("unused")
  11. Sheet sheet4 = wb.createSheet("rensanning");
  12. //保存为Excel文件
  13. FileOutputStream out = null;
  14. try {
  15. out = new FileOutputStream("c:\\text.xls");
  16. wb.write(out);
  17. } catch (IOException e) {
  18. System.out.println(e.toString());
  19. } finally {
  20. try {
  21. out.close();
  22. } catch (IOException e) {
  23. System.out.println(e.toString());
  24. }
  25. }
//生成Workbook
HSSFWorkbook wb = new HSSFWorkbook();

//添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
@SuppressWarnings("unused")
Sheet sheet1 = wb.createSheet();
@SuppressWarnings("unused")
Sheet sheet2 = wb.createSheet();
@SuppressWarnings("unused")
Sheet sheet3 = wb.createSheet("new sheet");
@SuppressWarnings("unused")
Sheet sheet4 = wb.createSheet("rensanning");

//保存为Excel文件
FileOutputStream out = null;

try {
    out = new FileOutputStream("c:\\text.xls");
    wb.write(out);		
} catch (IOException e) {
    System.out.println(e.toString());
} finally {
    try {
        out.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}	



2、生成Workbook OOXML形式(.xlsx)

 
Java代码 复制代码 收藏代码
  1. //生成Workbook
  2. XSSFWorkbook wb = new XSSFWorkbook();
  3. //......
//生成Workbook
XSSFWorkbook wb = new XSSFWorkbook();

//......



3、打开Workbook

 

Java代码 复制代码 收藏代码
  1. //方法一:使用WorkbookFactory
  2. FileInputStream in = null;
  3. Workbook wb = null;
  4. try {
  5. in = new FileInputStream(TEST_WORKBOOK_NAME);
  6. wb = WorkbookFactory.create(in);
  7. } catch (IOException e) {
  8. System.out.println(e.toString());
  9. } catch (InvalidFormatException e) {
  10. System.out.println(e.toString());
  11. } finally {
  12. try {
  13. in.close();
  14. } catch (IOException e) {
  15. System.out.println(e.toString());
  16. }
  17. }
  18. System.out.println("====================Workbook====================");
  19. System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
  20. System.out.println("Sheet3's name:" + wb.getSheetName(3));
  21. System.out.println();
  22. //方法二:使用POIFSFileSystem
  23. try {
  24. in = new FileInputStream(TEST_WORKBOOK_NAME);
  25. POIFSFileSystem fs = new POIFSFileSystem(in);
  26. wb = new HSSFWorkbook(fs);
  27. } catch (IOException e) {
  28. System.out.println(e.toString());
  29. } finally {
  30. try {
  31. in.close();
  32. } catch (IOException e) {
  33. System.out.println(e.toString());
  34. }
  35. }
  36. System.out.println("====================Workbook====================");
  37. System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
  38. System.out.println("Sheet3's name:" + wb.getSheetName(3));
  39. System.out.println();
//方法一:使用WorkbookFactory
FileInputStream in = null;
Workbook wb = null;

try {
	in = new FileInputStream(TEST_WORKBOOK_NAME);
	wb = WorkbookFactory.create(in);
} catch (IOException e) {
	System.out.println(e.toString());
} catch (InvalidFormatException e) {
	System.out.println(e.toString());
} finally {
	try {
		in.close();
	} catch (IOException e) {
		System.out.println(e.toString());
	}
}

System.out.println("====================Workbook====================");
System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
System.out.println("Sheet3's name:" + wb.getSheetName(3));
System.out.println();

//方法二:使用POIFSFileSystem
try {
	in = new FileInputStream(TEST_WORKBOOK_NAME);
	POIFSFileSystem fs = new POIFSFileSystem(in);
	wb = new HSSFWorkbook(fs);
} catch (IOException e) {
	System.out.println(e.toString());
} finally {
	try {
		in.close();
	} catch (IOException e) {
		System.out.println(e.toString());
	}
}

System.out.println("====================Workbook====================");
System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
System.out.println("Sheet3's name:" + wb.getSheetName(3));
System.out.println();



4、打开加密的Workbook(读加密)

 
Java代码 复制代码 收藏代码
  1. FileInputStream input = new FileInputStream(TEST_WORKBOOK_NAME_ENCRYPTED);
  2. BufferedInputStream binput = new BufferedInputStream(input);
  3. POIFSFileSystem poifs = new POIFSFileSystem(binput);
  4. Biff8EncryptionKey.setCurrentUserPassword(TEST_WORKBOOK_PASSWORD);
  5. HSSFWorkbook wb = new HSSFWorkbook(poifs);
  6. System.out.println("====================EncryptedWorkbook====================");
  7. System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
  8. System.out.println("Sheet0's name:" + wb.getSheetName(0));
  9. System.out.println();
FileInputStream input = new FileInputStream(TEST_WORKBOOK_NAME_ENCRYPTED);
BufferedInputStream binput = new BufferedInputStream(input);
POIFSFileSystem poifs = new POIFSFileSystem(binput);

Biff8EncryptionKey.setCurrentUserPassword(TEST_WORKBOOK_PASSWORD);

HSSFWorkbook wb = new HSSFWorkbook(poifs);

System.out.println("====================EncryptedWorkbook====================");
System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
System.out.println("Sheet0's name:" + wb.getSheetName(0));
System.out.println();



5、追加Sheet

 
Java代码 复制代码 收藏代码
  1. Sheet sheet = wb.createSheet("append sheet");
Sheet sheet = wb.createSheet("append sheet");



6、复制Sheet

 
Java代码 复制代码 收藏代码
  1. wb.cloneSheet(1);
wb.cloneSheet(1);



7、修改Sheet名称

 
Java代码 复制代码 收藏代码
  1. wb.setSheetName(i, "SheetName new");
wb.setSheetName(i, "SheetName new");



8、删除Sheet

 
Java代码 复制代码 收藏代码
  1. wb.removeSheetAt(1);
wb.removeSheetAt(1);



9、设置下部Sheet名的Tab的第一个可见Tab

 
Java代码 复制代码 收藏代码
  1. //设置下部Sheet名的Tab的第一个可见Tab(以左的Sheet看不见)
  2. wb.setFirstVisibleTab(2);
//设置下部Sheet名的Tab的第一个可见Tab(以左的Sheet看不见)
wb.setFirstVisibleTab(2);



10、调整Sheet顺序

 
Java代码 复制代码 收藏代码
  1. wb.setSheetOrder("SheetName3", 1);
  2. wb.setSheetOrder(wb.getSheetName(4), 0);
wb.setSheetOrder("SheetName3", 1);
wb.setSheetOrder(wb.getSheetName(4), 0);



11、设置当前Sheet

 
Java代码 复制代码 收藏代码
  1. //设置当前Sheet
  2. wb.setActiveSheet(wb.getNumberOfSheets() - 1);
  3. //(Excel的当前Sheet被设置,需要结合setSelected使用,不然下部Sheet名的Tab还是默认为第一个)
  4. //(需要选择多个Sheet的话,每个Sheet调用setSelected(true)即可)
  5. wb.getSheetAt(wb.getNumberOfSheets() - 1).setSelected(true);
//设置当前Sheet
wb.setActiveSheet(wb.getNumberOfSheets() - 1);
//(Excel的当前Sheet被设置,需要结合setSelected使用,不然下部Sheet名的Tab还是默认为第一个)
//(需要选择多个Sheet的话,每个Sheet调用setSelected(true)即可)
wb.getSheetAt(wb.getNumberOfSheets() - 1).setSelected(true);



12、固定窗口

 
Java代码 复制代码 收藏代码
  1. wb.getSheet("SheetName4").createFreezePane(2, 2);
wb.getSheet("SheetName4").createFreezePane(2, 2);


java 读取 excel_第1张图片

13、分割窗口

 
Java代码 复制代码 收藏代码
  1. wb.getSheet("SheetName5").createSplitPane(2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT);
wb.getSheet("SheetName5").createSplitPane(2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT);


java 读取 excel_第2张图片

14、Sheet缩放

 

Java代码 复制代码 收藏代码
  1. //setZoom(int numerator, int denominator)
  2. //"numerator"÷"denominator" 例如: 3÷1=3 那就是设置为300%
  3. //扩大(200%)
  4. wb.getSheet("sheetname1").setZoom(2, 1);
  5. //缩小(50%)
  6. wb.getSheet("sheetname2").setZoom(1, 2);
//setZoom(int numerator, int denominator)
//"numerator"÷"denominator"  例如: 3÷1=3 那就是设置为300%

//扩大(200%)
wb.getSheet("sheetname1").setZoom(2, 1);
//缩小(50%)
wb.getSheet("sheetname2").setZoom(1, 2);


java 读取 excel_第3张图片

15、行列分组

 

Java代码 复制代码 收藏代码
  1. wb.getSheet("sheetname3").groupColumn(4, 7);
  2. wb.getSheet("sheetname3").groupColumn(9, 12);
  3. wb.getSheet("sheetname3").groupColumn(10, 11);
  4. wb.getSheet("sheetname3").groupRow(5, 14);
  5. wb.getSheet("sheetname3").groupRow(7, 13);
  6. wb.getSheet("sheetname3").groupRow(16, 19);
wb.getSheet("sheetname3").groupColumn(4, 7);
wb.getSheet("sheetname3").groupColumn(9, 12);
wb.getSheet("sheetname3").groupColumn(10, 11);

wb.getSheet("sheetname3").groupRow(5, 14);
wb.getSheet("sheetname3").groupRow(7, 13);
wb.getSheet("sheetname3").groupRow(16, 19);


java 读取 excel_第4张图片

16、关闭分组

 
Java代码 复制代码 收藏代码
  1. wb.getSheet("sheetname3").setColumnGroupCollapsed(10, true);
  2. wb.getSheet("sheetname3").setRowGroupCollapsed(7, true);
wb.getSheet("sheetname3").setColumnGroupCollapsed(10, true);
wb.getSheet("sheetname3").setRowGroupCollapsed(7, true);



17、插入行

 
Java代码 复制代码 收藏代码
  1. Row row1 = wb.getSheet("sheetname4").createRow(1);
  2. Cell cell1_1 = row1.createCell(1);
  3. cell1_1.setCellValue(123);
  4. Row row4 = wb.getSheet("sheetname4").createRow(4);
  5. Cell cell4_3 = row4.createCell(3);
  6. cell4_3.setCellValue("中国");
Row row1 = wb.getSheet("sheetname4").createRow(1);
Cell cell1_1 = row1.createCell(1);
cell1_1.setCellValue(123);

Row row4 = wb.getSheet("sheetname4").createRow(4);
Cell cell4_3 = row4.createCell(3);
cell4_3.setCellValue("中国");



18、删除行

 
Java代码 复制代码 收藏代码
  1. Row row = wb.getSheet("sheetname4").getRow(1);
  2. wb.getSheet("sheetname4").removeRow(row);
Row row = wb.getSheet("sheetname4").getRow(1);
wb.getSheet("sheetname4").removeRow(row);



19、移动行

 
Java代码 复制代码 收藏代码
  1. //******移动行只移动内容,不牵扯行的删除和插入
  2. //移动行(把第1行和第2行移到第5行之后)
  3. wb.getSheet("sheetname5").shiftRows(0, 1, 5);
  4. //移动行(把第3行和第4行往上移动1行)
  5. wb.getSheet("sheetname5").shiftRows(2, 3, -1);
//******移动行只移动内容,不牵扯行的删除和插入

//移动行(把第1行和第2行移到第5行之后)
wb.getSheet("sheetname5").shiftRows(0, 1, 5);

//移动行(把第3行和第4行往上移动1行)
wb.getSheet("sheetname5").shiftRows(2, 3, -1);



20、修改行高

 
Java代码 复制代码 收藏代码
  1. //设置默认行高
  2. wb.getSheet("sheetname6").setDefaultRowHeight((short)100);
  3. //设置行高
  4. wb.getSheet("sheetname6").getRow(2).setHeight((short)(100 * 20));
//设置默认行高
wb.getSheet("sheetname6").setDefaultRowHeight((short)100);

//设置行高
wb.getSheet("sheetname6").getRow(2).setHeight((short)(100 * 20));



21、修改列宽

 
Java代码 复制代码 收藏代码
  1. //设置默认列宽
  2. wb.getSheet("sheetname7").setDefaultColumnWidth(12);
  3. //设置列宽
  4. wb.getSheet("sheetname7").setColumnWidth(0, 5 * 256);
//设置默认列宽
wb.getSheet("sheetname7").setDefaultColumnWidth(12);

//设置列宽
wb.getSheet("sheetname7").setColumnWidth(0, 5 * 256);



22、不显示网格线

 

Java代码 复制代码 收藏代码
  1. //不显示网格线
  2. wb.getSheet("sheetname8").setDisplayGridlines(false);
//不显示网格线
wb.getSheet("sheetname8").setDisplayGridlines(false);


java 读取 excel_第5张图片

23、设置分页

 

 

Java代码 复制代码 收藏代码
  1. //设置第一页:3行2列 (可以多次设置)
  2. wb.getSheet("sheetname9").setRowBreak(2);
  3. wb.getSheet("sheetname9").setColumnBreak(1);
//设置第一页:3行2列 (可以多次设置)
wb.getSheet("sheetname9").setRowBreak(2);
wb.getSheet("sheetname9").setColumnBreak(1);


java 读取 excel_第6张图片

24、添加,删除,合并单元格

 
Java代码 复制代码 收藏代码
  1. //追加行
  2. for (int i = 0; i < 10; i++) {
  3. Row row = wb.getSheet("sheetname10").createRow(i);
  4. for (int j = 0; j < 10; j++) {
  5. //添加单元格
  6. Cell cell = row.createCell(j);
  7. cell.setCellValue(i + 1);
  8. }
  9. //删除单元格
  10. row.removeCell(row.getCell(5));
  11. }
  12. //合并单元格
  13. //CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
  14. wb.getSheet("sheetname10").addMergedRegion(new CellRangeAddress(1, 4, 2, 3));
//追加行
for (int i = 0; i < 10; i++) {
    Row row = wb.getSheet("sheetname10").createRow(i);
    for (int j = 0; j < 10; j++) {
    	//添加单元格
	    Cell cell = row.createCell(j);
	    cell.setCellValue(i + 1);
    }
    
    //删除单元格
    row.removeCell(row.getCell(5));
}
	    
//合并单元格
//CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
wb.getSheet("sheetname10").addMergedRegion(new CellRangeAddress(1, 4, 2, 3));


java 读取 excel_第7张图片

25、设置Header,Footer

 
Java代码 复制代码 收藏代码
  1. //Header
  2. Header header = wb.getSheet("sheetname11").getHeader();
  3. header.setLeft(HSSFHeader.startUnderline() +
  4. HSSFHeader.font("宋体", "Italic") +
  5. "文字文字" +
  6. HSSFHeader.endUnderline());
  7. header.setCenter(HSSFHeader.fontSize((short)16) +
  8. HSSFHeader.startDoubleUnderline() +
  9. HSSFHeader.startBold() +
  10. "汉字汉字" +
  11. HSSFHeader.endBold() +
  12. HSSFHeader.endDoubleUnderline());
  13. header.setRight("打印时间:" + HSSFHeader.date() + " " + HSSFHeader.time());
  14. //Footer
  15. Footer footer = wb.getSheet("sheetname11").getFooter();
  16. footer.setLeft("Copyright @ rensanning");
  17. footer.setCenter("Page:" + HSSFFooter.page() + " / " + HSSFFooter.numPages());
  18. footer.setRight("File:" + HSSFFooter.file());
//Header
Header header = wb.getSheet("sheetname11").getHeader();
header.setLeft(HSSFHeader.startUnderline() +
        HSSFHeader.font("宋体", "Italic") +
        "文字文字" +
        HSSFHeader.endUnderline());
header.setCenter(HSSFHeader.fontSize((short)16) +
        HSSFHeader.startDoubleUnderline() +
        HSSFHeader.startBold() +
        "汉字汉字" +
        HSSFHeader.endBold() +
        HSSFHeader.endDoubleUnderline());
header.setRight("打印时间:" + HSSFHeader.date() +  " " + HSSFHeader.time());

//Footer
Footer footer = wb.getSheet("sheetname11").getFooter();
footer.setLeft("Copyright @ rensanning");
footer.setCenter("Page:" + HSSFFooter.page() + " / " + HSSFFooter.numPages());
footer.setRight("File:" + HSSFFooter.file());





26、设置单元格值

 
Java代码 复制代码 收藏代码
  1. //boolean
  2. Cell cell00 = rows[0].createCell(0);
  3. boolean val00 = true;
  4. cell00.setCellValue(val00);
  5. //Calendar 格式化
  6. CellStyle styleCalendar = wb.createCellStyle();
  7. DataFormat formatCalendar = wb.createDataFormat();
  8. styleCalendar.setDataFormat(formatCalendar.getFormat("yyyy/mm/dd"));
  9. Cell cell11 = rows[1].createCell(0);
  10. Calendar val11 = Calendar.getInstance();
  11. cell11.setCellStyle(styleCalendar);
  12. cell11.setCellValue(val11);
  13. //Date 格式化
  14. CellStyle styleDate = wb.createCellStyle();
  15. DataFormat formatDate = wb.createDataFormat();
  16. styleDate.setDataFormat(formatDate.getFormat("yyyy/mm/dd hh:mm"));
  17. Cell cell21 = rows[2].createCell(0);
  18. Date val21 = new Date();
  19. cell21.setCellStyle(styleDate);
  20. cell21.setCellValue(val21);
  21. //double
  22. Cell cell30 = rows[3].createCell(0);
  23. double val30 = 1234.56;
  24. cell30.setCellValue(val30);
  25. //double 格式化
  26. CellStyle styleDouble = wb.createCellStyle();
  27. DataFormat formatDouble = wb.createDataFormat();
  28. styleDouble.setDataFormat(formatDouble.getFormat("#,##0.00"));
  29. Cell cell31 = rows[3].createCell(1);
  30. double val31 = 1234.56;
  31. cell31.setCellStyle(styleDouble);
  32. cell31.setCellValue(val31);
  33. //String
  34. Cell cell40 = rows[4].createCell(0);
  35. HSSFRichTextString val40 = new HSSFRichTextString("Test汉字");
  36. cell40.setCellValue(val40);
//boolean
Cell cell00 = rows[0].createCell(0);
boolean val00 = true;
cell00.setCellValue(val00);

//Calendar 格式化
CellStyle styleCalendar = wb.createCellStyle();
DataFormat formatCalendar = wb.createDataFormat();
styleCalendar.setDataFormat(formatCalendar.getFormat("yyyy/mm/dd"));
Cell cell11 = rows[1].createCell(0);
Calendar val11 = Calendar.getInstance();
cell11.setCellStyle(styleCalendar);
cell11.setCellValue(val11);

//Date 格式化
CellStyle styleDate = wb.createCellStyle();
DataFormat formatDate = wb.createDataFormat();
styleDate.setDataFormat(formatDate.getFormat("yyyy/mm/dd hh:mm"));
Cell cell21 = rows[2].createCell(0);
Date val21 = new Date();
cell21.setCellStyle(styleDate);
cell21.setCellValue(val21);

//double
Cell cell30 = rows[3].createCell(0);
double val30 = 1234.56;
cell30.setCellValue(val30);

//double 格式化
CellStyle styleDouble = wb.createCellStyle();
DataFormat formatDouble = wb.createDataFormat();
styleDouble.setDataFormat(formatDouble.getFormat("#,##0.00"));
Cell cell31 = rows[3].createCell(1);
double val31 = 1234.56;
cell31.setCellStyle(styleDouble);
cell31.setCellValue(val31);

//String
Cell cell40 = rows[4].createCell(0);
HSSFRichTextString val40 = new HSSFRichTextString("Test汉字");
cell40.setCellValue(val40);


java 读取 excel_第8张图片

27、设置单元格边线

 

Java代码 复制代码 收藏代码
  1. wb.getSheet("sheetname2").setColumnWidth(1, 4096);
  2. Row row1 = wb.getSheet("sheetname2").createRow(1);
  3. row1.setHeightInPoints(70);
  4. Cell cell1_1 = row1.createCell(1);
  5. cell1_1.setCellValue("Sample");
  6. CellStyle style = wb.createCellStyle();
  7. style.setBorderTop(CellStyle.BORDER_DASHED);
  8. style.setBorderBottom(CellStyle.BORDER_DOUBLE);
  9. style.setBorderLeft(CellStyle.BORDER_MEDIUM_DASH_DOT);
  10. style.setBorderRight(CellStyle.BORDER_MEDIUM);
  11. style.setTopBorderColor(IndexedColors.MAROON.getIndex());
  12. style.setBottomBorderColor(IndexedColors.SKY_BLUE.getIndex());
  13. style.setLeftBorderColor(IndexedColors.ORANGE.getIndex());
  14. style.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
  15. cell1_1.setCellStyle(style);
wb.getSheet("sheetname2").setColumnWidth(1, 4096);

Row row1 = wb.getSheet("sheetname2").createRow(1);
row1.setHeightInPoints(70);

Cell cell1_1 = row1.createCell(1);
cell1_1.setCellValue("Sample");

CellStyle style = wb.createCellStyle();

style.setBorderTop(CellStyle.BORDER_DASHED);
style.setBorderBottom(CellStyle.BORDER_DOUBLE);
style.setBorderLeft(CellStyle.BORDER_MEDIUM_DASH_DOT);
style.setBorderRight(CellStyle.BORDER_MEDIUM);

style.setTopBorderColor(IndexedColors.MAROON.getIndex());
style.setBottomBorderColor(IndexedColors.SKY_BLUE.getIndex());
style.setLeftBorderColor(IndexedColors.ORANGE.getIndex());
style.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());

cell1_1.setCellStyle(style);


java 读取 excel_第9张图片

28、设置单元格背景填充

 
Java代码 复制代码 收藏代码
  1. wb.getSheet("sheetname3").setColumnWidth(0, 4096);
  2. wb.getSheet("sheetname3").setColumnWidth(1, 4096);
  3. wb.getSheet("sheetname3").setColumnWidth(2, 4096);
  4. Row row1 = wb.getSheet("sheetname3").createRow(1);
  5. row1.setHeightInPoints(70);
  6. Cell cell1_0 = row1.createCell(0);
  7. Cell cell1_1 = row1.createCell(1);
  8. Cell cell1_2 = row1.createCell(2);
  9. cell1_0.setCellValue("THIN_VERT_BANDS");
  10. cell1_1.setCellValue("BIG_SPOTS");
  11. cell1_2.setCellValue("THICK_HORZ_BANDS");
  12. CellStyle style1 = wb.createCellStyle();
  13. style1.setFillPattern(CellStyle.THIN_VERT_BANDS);
  14. style1.setFillForegroundColor(IndexedColors.WHITE.getIndex());
  15. style1.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
  16. CellStyle style2 = wb.createCellStyle();
  17. style2.setFillPattern(CellStyle.BIG_SPOTS);
  18. style2.setFillForegroundColor(IndexedColors.RED.getIndex());
  19. style2.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
  20. CellStyle style3 = wb.createCellStyle();
  21. style3.setFillPattern(CellStyle.THICK_HORZ_BANDS);
  22. style3.setFillForegroundColor(IndexedColors.PINK.getIndex());
  23. style3.setFillBackgroundColor(IndexedColors.BROWN.getIndex());
  24. cell1_0.setCellStyle(style1);
  25. cell1_1.setCellStyle(style2);
  26. cell1_2.setCellStyle(style3);
wb.getSheet("sheetname3").setColumnWidth(0, 4096);
wb.getSheet("sheetname3").setColumnWidth(1, 4096);
wb.getSheet("sheetname3").setColumnWidth(2, 4096);

Row row1 = wb.getSheet("sheetname3").createRow(1);
row1.setHeightInPoints(70);

Cell cell1_0 = row1.createCell(0);
Cell cell1_1 = row1.createCell(1);
Cell cell1_2 = row1.createCell(2);

cell1_0.setCellValue("THIN_VERT_BANDS");
cell1_1.setCellValue("BIG_SPOTS");
cell1_2.setCellValue("THICK_HORZ_BANDS");

CellStyle style1 = wb.createCellStyle();
style1.setFillPattern(CellStyle.THIN_VERT_BANDS);
style1.setFillForegroundColor(IndexedColors.WHITE.getIndex());
style1.setFillBackgroundColor(IndexedColors.BLUE.getIndex());

CellStyle style2 = wb.createCellStyle();
style2.setFillPattern(CellStyle.BIG_SPOTS);
style2.setFillForegroundColor(IndexedColors.RED.getIndex());
style2.setFillBackgroundColor(IndexedColors.WHITE.getIndex());

CellStyle style3 = wb.createCellStyle();
style3.setFillPattern(CellStyle.THICK_HORZ_BANDS);
style3.setFillForegroundColor(IndexedColors.PINK.getIndex());
style3.setFillBackgroundColor(IndexedColors.BROWN.getIndex());

cell1_0.setCellStyle(style1);
cell1_1.setCellStyle(style2);
cell1_2.setCellStyle(style3);


java 读取 excel_第10张图片

29、设置单元格注释

 

Java代码 复制代码 收藏代码
  1. HSSFCreationHelper createHelper =
  2. (HSSFCreationHelper)wb.getCreationHelper();
  3. Drawing patriarch = wb.getSheet("sheetname4").createDrawingPatriarch();
  4. //注释
  5. Row row = wb.getSheet("sheetname4").createRow(1);
  6. Cell cell = row.createCell(1);
  7. HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 0, 0, 0,
  8. (short) 4, 2, (short) 6, 5);
  9. Comment comment = patriarch.createCellComment(clientAnchor);
  10. comment.setString(createHelper.createRichTextString("注释注释111"));
  11. comment.setAuthor("rensanning");
  12. cell.setCellComment(comment);
  13. //带字体的注释
  14. Row row2 = wb.getSheet("sheetname4").createRow(2);
  15. Cell cell2 = row2.createCell(1);
  16. Font font = wb.createFont();
  17. font.setFontName("宋体");
  18. font.setFontHeightInPoints((short)10);
  19. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  20. font.setColor(HSSFColor.RED.index);
  21. Comment comment2 = patriarch.createCellComment(clientAnchor);
  22. HSSFRichTextString text = new HSSFRichTextString("注释注释222");
  23. text.applyFont(font);
  24. comment2.setString(text);
  25. comment2.setAuthor("rensanning");
  26. cell2.setCellComment(comment2);
HSSFCreationHelper createHelper =
    (HSSFCreationHelper)wb.getCreationHelper();
Drawing patriarch = wb.getSheet("sheetname4").createDrawingPatriarch();

//注释
Row row = wb.getSheet("sheetname4").createRow(1);
Cell cell = row.createCell(1);

HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 0, 0, 0,
		(short) 4, 2, (short) 6, 5);

Comment comment = patriarch.createCellComment(clientAnchor);
comment.setString(createHelper.createRichTextString("注释注释111"));
comment.setAuthor("rensanning");

cell.setCellComment(comment);

//带字体的注释
Row row2 = wb.getSheet("sheetname4").createRow(2);
Cell cell2 = row2.createCell(1);

Font font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.RED.index);

Comment comment2 = patriarch.createCellComment(clientAnchor);
HSSFRichTextString text = new HSSFRichTextString("注释注释222");
text.applyFont(font);
comment2.setString(text);
comment2.setAuthor("rensanning");

cell2.setCellComment(comment2);



30、设置单元格字体(斜体,粗体,下线,取消线,字体,大小,背景色)

 

Java代码 复制代码 收藏代码
  1. Font font = null;
  2. CellStyle style = null;
  3. //斜体
  4. font = wb.createFont();
  5. font.setItalic(true);
  6. style = wb.createCellStyle();
  7. style.setFont(font);
  8. wb.getSheet("sheetname5").getRow(1).getCell(1).setCellStyle(style);
  9. //粗体
  10. font = wb.createFont();
  11. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  12. style = wb.createCellStyle();
  13. style.setFont(font);
  14. wb.getSheet("sheetname5").getRow(2).getCell(1).setCellStyle(style);
  15. //字体名
  16. font = wb.createFont();
  17. font.setFontName("Courier New");
  18. style = wb.createCellStyle();
  19. style.setFont(font);
  20. wb.getSheet("sheetname5").getRow(3).getCell(1).setCellStyle(style);
  21. //字体大小
  22. font = wb.createFont();
  23. font.setFontHeightInPoints((short)20);
  24. style = wb.createCellStyle();
  25. style.setFont(font);
  26. wb.getSheet("sheetname5").getRow(4).getCell(1).setCellStyle(style);
  27. //文字颜色
  28. font = wb.createFont();
  29. font.setColor(HSSFColor.YELLOW.index);
  30. style = wb.createCellStyle();
  31. style.setFont(font);
  32. wb.getSheet("sheetname5").getRow(5).getCell(1).setCellStyle(style);
  33. //上标
  34. font = wb.createFont();
  35. font.setTypeOffset(HSSFFont.SS_SUPER);
  36. style = wb.createCellStyle();
  37. style.setFont(font);
  38. wb.getSheet("sheetname5").getRow(6).getCell(1).setCellStyle(style);
  39. //下标
  40. font = wb.createFont();
  41. font.setTypeOffset(HSSFFont.SS_SUB);
  42. style = wb.createCellStyle();
  43. style.setFont(font);
  44. wb.getSheet("sheetname5").getRow(7).getCell(1).setCellStyle(style);
  45. //删除线
  46. font = wb.createFont();
  47. font.setStrikeout(true);
  48. style = wb.createCellStyle();
  49. style.setFont(font);
  50. wb.getSheet("sheetname5").getRow(8).getCell(1).setCellStyle(style);
  51. //下划线
  52. font = wb.createFont();
  53. font.setUnderline(HSSFFont.U_SINGLE);
  54. style = wb.createCellStyle();
  55. style.setFont(font);
  56. wb.getSheet("sheetname5").getRow(9).getCell(1).setCellStyle(style);
  57. //背景色
  58. style = wb.createCellStyle();
  59. style.setFillForegroundColor(HSSFColor.SEA_GREEN.index);
  60. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  61. style.setFont(font);
  62. wb.getSheet("sheetname5").getRow(10).getCell(1).setCellStyle(style);
Font font = null;
CellStyle style = null;

//斜体
font = wb.createFont();
font.setItalic(true);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(1).getCell(1).setCellStyle(style);

//粗体
font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(2).getCell(1).setCellStyle(style);

//字体名
font = wb.createFont();
font.setFontName("Courier New");
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(3).getCell(1).setCellStyle(style);

//字体大小
font = wb.createFont();
font.setFontHeightInPoints((short)20);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(4).getCell(1).setCellStyle(style);

//文字颜色
font = wb.createFont();
font.setColor(HSSFColor.YELLOW.index);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(5).getCell(1).setCellStyle(style);
        
//上标
font = wb.createFont();
font.setTypeOffset(HSSFFont.SS_SUPER);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(6).getCell(1).setCellStyle(style);

//下标
font = wb.createFont();
font.setTypeOffset(HSSFFont.SS_SUB);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(7).getCell(1).setCellStyle(style);

//删除线
font = wb.createFont();
font.setStrikeout(true);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(8).getCell(1).setCellStyle(style);

//下划线
font = wb.createFont();
font.setUnderline(HSSFFont.U_SINGLE);
style = wb.createCellStyle();
style.setFont(font);

wb.getSheet("sheetname5").getRow(9).getCell(1).setCellStyle(style);

//背景色
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.SEA_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style.setFont(font);

wb.getSheet("sheetname5").getRow(10).getCell(1).setCellStyle(style);


java 读取 excel_第11张图片

31、设置超链接

 

Java代码 复制代码 收藏代码
  1. HSSFCreationHelper createHelper =
  2. (HSSFCreationHelper)wb.getCreationHelper();
  3. CellStyle style = wb.createCellStyle();
  4. Font font = wb.createFont();
  5. font.setUnderline(HSSFFont.U_SINGLE);
  6. font.setColor(HSSFColor.BLUE.index);
  7. style.setFont(font);
  8. //追加行
  9. Row[] rows = new Row[10];
  10. for (int i = 0; i < 10; i++) {
  11. rows[i] = wb.getSheet("sheetname6").createRow(i);
  12. }
  13. //URL
  14. rows[0].createCell(0).setCellValue("URL Link");
  15. HSSFHyperlink link1 = createHelper.createHyperlink(HSSFHyperlink.LINK_URL);
  16. link1.setAddress("http://poi.apache.org/");
  17. rows[0].getCell(0).setHyperlink(link1);
  18. rows[0].getCell(0).setCellStyle(style);
  19. //Mail
  20. rows[1].createCell(0).setCellValue("Email Link");
  21. HSSFHyperlink link2 = createHelper.createHyperlink(HSSFHyperlink.LINK_EMAIL);
  22. link2.setAddress("mailto:[email protected]?subject=Hyperlinks");
  23. rows[1].getCell(0).setHyperlink(link2);
  24. rows[1].getCell(0).setCellStyle(style);
  25. //File
  26. rows[2].createCell(0).setCellValue("File Link");
  27. HSSFHyperlink link3 = createHelper.createHyperlink(HSSFHyperlink.LINK_FILE);
  28. link3.setAddress("link.xls");
  29. rows[2].getCell(0).setHyperlink(link3);
  30. rows[2].getCell(0).setCellStyle(style);
  31. //Workbook内
  32. rows[3].createCell(0).setCellValue("Worksheet Link");
  33. HSSFHyperlink link4 = createHelper.createHyperlink(HSSFHyperlink.LINK_DOCUMENT);
  34. link4.setAddress("sheetname1!A1");
  35. rows[3].getCell(0).setHyperlink(link4);
  36. rows[3].getCell(0).setCellStyle(style);
HSSFCreationHelper createHelper =
    (HSSFCreationHelper)wb.getCreationHelper();

CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setUnderline(HSSFFont.U_SINGLE);
font.setColor(HSSFColor.BLUE.index);
style.setFont(font);
        
//追加行
Row[] rows = new Row[10];
for (int i = 0; i < 10; i++) {
	rows[i] = wb.getSheet("sheetname6").createRow(i);
}

//URL
rows[0].createCell(0).setCellValue("URL Link");

HSSFHyperlink link1 = createHelper.createHyperlink(HSSFHyperlink.LINK_URL);
link1.setAddress("http://poi.apache.org/");
rows[0].getCell(0).setHyperlink(link1);
rows[0].getCell(0).setCellStyle(style);

//Mail
rows[1].createCell(0).setCellValue("Email Link");

HSSFHyperlink link2 = createHelper.createHyperlink(HSSFHyperlink.LINK_EMAIL);
link2.setAddress("mailto:[email protected]?subject=Hyperlinks");
rows[1].getCell(0).setHyperlink(link2);
rows[1].getCell(0).setCellStyle(style);

//File
rows[2].createCell(0).setCellValue("File Link");

HSSFHyperlink link3 = createHelper.createHyperlink(HSSFHyperlink.LINK_FILE);
link3.setAddress("link.xls");
rows[2].getCell(0).setHyperlink(link3);
rows[2].getCell(0).setCellStyle(style);

//Workbook内
rows[3].createCell(0).setCellValue("Worksheet Link");

HSSFHyperlink link4 = createHelper.createHyperlink(HSSFHyperlink.LINK_DOCUMENT);
link4.setAddress("sheetname1!A1");
rows[3].getCell(0).setHyperlink(link4);
rows[3].getCell(0).setCellStyle(style);



32、设置单元格横向对齐,纵向对齐

 

Java代码 复制代码 收藏代码
  1. //横向对齐
  2. wb.getSheet("sheetname7").setColumnWidth(2, 3072);
  3. Row[] row = new Row[7];
  4. Cell[] cell = new Cell[7];
  5. for (int i = 0 ; i < 7 ; i++){
  6. row[i] = wb.getSheet("sheetname7").createRow(i + 1);
  7. cell[i] = row[i].createCell(2);
  8. cell[i].setCellValue("Please give me a receipt");
  9. }
  10. CellStyle style0 = wb.createCellStyle();
  11. style0.setAlignment(CellStyle.ALIGN_GENERAL);
  12. cell[0].setCellStyle(style0);
  13. CellStyle style1 = wb.createCellStyle();
  14. style1.setAlignment(CellStyle.ALIGN_LEFT);
  15. cell[1].setCellStyle(style1);
  16. CellStyle style2 = wb.createCellStyle();
  17. style2.setAlignment(CellStyle.ALIGN_CENTER);
  18. cell[2].setCellStyle(style2);
  19. CellStyle style3 = wb.createCellStyle();
  20. style3.setAlignment(CellStyle.ALIGN_RIGHT);
  21. cell[3].setCellStyle(style3);
  22. CellStyle style4 = wb.createCellStyle();
  23. style4.setAlignment(CellStyle.ALIGN_FILL);
  24. cell[4].setCellStyle(style4);
  25. CellStyle style5 = wb.createCellStyle();
  26. style5.setAlignment(CellStyle.ALIGN_JUSTIFY);
  27. cell[5].setCellStyle(style5);
  28. CellStyle style6 = wb.createCellStyle();
  29. style6.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);
  30. cell[6].setCellStyle(style6);
  31. //纵向对齐
  32. Row row2 = wb.getSheet("sheetname8").createRow(1);
  33. row2.setHeightInPoints(70);
  34. Cell[] cell2 = new Cell[4];
  35. for (int i = 0 ; i < 4 ; i++){
  36. cell2[i] = row2.createCell(i + 1);
  37. cell2[i].setCellValue("Please give me a receipt");
  38. }
  39. CellStyle style02 = wb.createCellStyle();
  40. style02.setVerticalAlignment(CellStyle.VERTICAL_TOP);
  41. cell2[0].setCellStyle(style02);
  42. CellStyle style12 = wb.createCellStyle();
  43. style12.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  44. cell2[1].setCellStyle(style12);
  45. CellStyle style22 = wb.createCellStyle();
  46. style22.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
  47. cell2[2].setCellStyle(style22);
  48. CellStyle style32 = wb.createCellStyle();
  49. style32.setVerticalAlignment(CellStyle.VERTICAL_JUSTIFY);
  50. cell2[3].setCellStyle(style32);
//横向对齐
wb.getSheet("sheetname7").setColumnWidth(2, 3072);

Row[] row = new Row[7];
Cell[] cell = new Cell[7];

for (int i = 0 ; i < 7 ; i++){
  row[i] = wb.getSheet("sheetname7").createRow(i + 1);
  cell[i] = row[i].createCell(2);
  cell[i].setCellValue("Please give me a receipt");
}

CellStyle style0 = wb.createCellStyle();
style0.setAlignment(CellStyle.ALIGN_GENERAL);
cell[0].setCellStyle(style0);

CellStyle style1 = wb.createCellStyle();
style1.setAlignment(CellStyle.ALIGN_LEFT);
cell[1].setCellStyle(style1);

CellStyle style2 = wb.createCellStyle();
style2.setAlignment(CellStyle.ALIGN_CENTER);
cell[2].setCellStyle(style2);

CellStyle style3 = wb.createCellStyle();
style3.setAlignment(CellStyle.ALIGN_RIGHT);
cell[3].setCellStyle(style3);

CellStyle style4 = wb.createCellStyle();
style4.setAlignment(CellStyle.ALIGN_FILL);
cell[4].setCellStyle(style4);

CellStyle style5 = wb.createCellStyle();
style5.setAlignment(CellStyle.ALIGN_JUSTIFY);
cell[5].setCellStyle(style5);

CellStyle style6 = wb.createCellStyle();
style6.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);
cell[6].setCellStyle(style6);

//纵向对齐
Row row2 = wb.getSheet("sheetname8").createRow(1);
row2.setHeightInPoints(70);
Cell[] cell2 = new Cell[4];

for (int i = 0 ; i < 4 ; i++){
	cell2[i] = row2.createCell(i + 1);
	cell2[i].setCellValue("Please give me a receipt");
}

CellStyle style02 = wb.createCellStyle();
style02.setVerticalAlignment(CellStyle.VERTICAL_TOP);
cell2[0].setCellStyle(style02);

CellStyle style12 = wb.createCellStyle();
style12.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cell2[1].setCellStyle(style12);

CellStyle style22 = wb.createCellStyle();
style22.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
cell2[2].setCellStyle(style22);

CellStyle style32 = wb.createCellStyle();
style32.setVerticalAlignment(CellStyle.VERTICAL_JUSTIFY);
cell2[3].setCellStyle(style32);


java 读取 excel_第12张图片

33、设置单元格旋转角度

 

Java代码 复制代码 收藏代码
  1. Row[] row = new Row[4];
  2. Cell[] cell = new Cell[4];
  3. for (int i = 0 ; i < 4 ; i++){
  4. row[i] = wb.getSheet("sheetname9").createRow(i + 1);
  5. cell[i] = row[i].createCell(2);
  6. cell[i].setCellValue("Coffee");
  7. }
  8. CellStyle style0 = wb.createCellStyle();
  9. style0.setRotation((short)45);
  10. cell[0].setCellStyle(style0);
  11. CellStyle style1 = wb.createCellStyle();
  12. style1.setRotation((short)0);
  13. cell[1].setCellStyle(style1);
  14. CellStyle style2 = wb.createCellStyle();
  15. style2.setRotation((short)-45);
  16. cell[2].setCellStyle(style2);
  17. CellStyle style3 = wb.createCellStyle();
  18. style3.setRotation((short)-90);
  19. cell[3].setCellStyle(style3);
Row[] row = new Row[4];
Cell[] cell = new Cell[4];

for (int i = 0 ; i < 4 ; i++){
  row[i] = wb.getSheet("sheetname9").createRow(i + 1);
  cell[i] = row[i].createCell(2);
  cell[i].setCellValue("Coffee");
}

CellStyle style0 = wb.createCellStyle();
style0.setRotation((short)45);
cell[0].setCellStyle(style0);

CellStyle style1 = wb.createCellStyle();
style1.setRotation((short)0);
cell[1].setCellStyle(style1);

CellStyle style2 = wb.createCellStyle();
style2.setRotation((short)-45);
cell[2].setCellStyle(style2);

CellStyle style3 = wb.createCellStyle();
style3.setRotation((short)-90);
cell[3].setCellStyle(style3);


java 读取 excel_第13张图片

34、设置单元格自动折行

 

Java代码 复制代码 收藏代码
  1. Row[] row = new Row[2];
  2. Cell[] cell = new Cell[2];
  3. for (int i = 0 ; i < 2 ; i++){
  4. row[i] = wb.getSheet("sheetname10").createRow(i + 1);
  5. cell[i] = row[i].createCell(2);
  6. cell[i].setCellValue("Thank you very much.");
  7. }
  8. CellStyle style0 = wb.createCellStyle();
  9. style0.setWrapText(true);
  10. cell[0].setCellStyle(style0);
  11. CellStyle style1 = wb.createCellStyle();
  12. style1.setWrapText(false);
  13. cell[1].setCellStyle(style1);
Row[] row = new Row[2];
Cell[] cell = new Cell[2];

for (int i = 0 ; i < 2 ; i++){
  row[i] = wb.getSheet("sheetname10").createRow(i + 1);
  cell[i] = row[i].createCell(2);
  cell[i].setCellValue("Thank you very much.");
}

CellStyle style0 = wb.createCellStyle();
style0.setWrapText(true);
cell[0].setCellStyle(style0);

CellStyle style1 = wb.createCellStyle();
style1.setWrapText(false);
cell[1].setCellStyle(style1);


java 读取 excel_第14张图片

35、设置单元格文字缩进

 
Java代码 复制代码 收藏代码
  1. Row[] row = new Row[4];
  2. Cell[] cell = new Cell[4];
  3. for (int i = 0 ; i < 4 ; i++){
  4. row[i] = wb.getSheet("sheetname11").createRow(i + 1);
  5. cell[i] = row[i].createCell(2);
  6. cell[i].setCellValue("Coffee");
  7. }
  8. CellStyle style1 = wb.createCellStyle();
  9. style1.setIndention((short)1);
  10. style1.setAlignment(CellStyle.ALIGN_LEFT);
  11. cell[1].setCellStyle(style1);
  12. CellStyle style2 = wb.createCellStyle();
  13. style2.setIndention((short)2);
  14. style2.setAlignment(CellStyle.ALIGN_LEFT);
  15. cell[2].setCellStyle(style2);
  16. CellStyle style3 = wb.createCellStyle();
  17. style3.setIndention((short)3);
  18. style3.setAlignment(CellStyle.ALIGN_LEFT);
  19. cell[3].setCellStyle(style3);
Row[] row = new Row[4];
Cell[] cell = new Cell[4];

for (int i = 0 ; i < 4 ; i++){
  row[i] = wb.getSheet("sheetname11").createRow(i + 1);
  cell[i] = row[i].createCell(2);
  cell[i].setCellValue("Coffee");
}

CellStyle style1 = wb.createCellStyle();
style1.setIndention((short)1);
style1.setAlignment(CellStyle.ALIGN_LEFT);
cell[1].setCellStyle(style1);

CellStyle style2 = wb.createCellStyle();
style2.setIndention((short)2);
style2.setAlignment(CellStyle.ALIGN_LEFT);
cell[2].setCellStyle(style2);

CellStyle style3 = wb.createCellStyle();
style3.setIndention((short)3);
style3.setAlignment(CellStyle.ALIGN_LEFT);
cell[3].setCellStyle(style3);


java 读取 excel_第15张图片

36、自定义格式

 
Java代码 复制代码 收藏代码
  1. Row[] rows = new Row[2];
  2. for (int i = 0; i < rows.length; i++) {
  3. rows[i] = wb.getSheet("sheetname12").createRow(i + 1);
  4. }
  5. DataFormat format = wb.createDataFormat();
  6. CellStyle[] styles = new CellStyle[2];
  7. for (int i = 0; i < styles.length; i++) {
  8. styles[i] = wb.createCellStyle();
  9. }
  10. styles[0].setDataFormat(format.getFormat("0.0"));
  11. styles[1].setDataFormat(format.getFormat("#,##0.000"));
  12. Cell[] cells = new Cell[2];
  13. for (int i = 0; i < cells.length; i++) {
  14. cells[i] = rows[i].createCell(1);
  15. cells[i].setCellValue(1111.25);
  16. cells[i].setCellStyle(styles[i]);
  17. }
Row[] rows = new Row[2];
for (int i = 0; i < rows.length; i++) {
    rows[i] = wb.getSheet("sheetname12").createRow(i + 1);
}
DataFormat format = wb.createDataFormat();

CellStyle[] styles = new CellStyle[2];
for (int i = 0; i < styles.length; i++) {
    styles[i] = wb.createCellStyle();
}
styles[0].setDataFormat(format.getFormat("0.0"));
styles[1].setDataFormat(format.getFormat("#,##0.000"));

Cell[] cells = new Cell[2];
for (int i = 0; i < cells.length; i++)  {
    cells[i] = rows[i].createCell(1);
    cells[i].setCellValue(1111.25);

    cells[i].setCellStyle(styles[i]);
}


java 读取 excel_第16张图片

37、设置公式

 
Java代码 复制代码 收藏代码
  1. Row row1 = wb.getSheet("sheetname13").createRow(1);
  2. Row row2 = wb.getSheet("sheetname13").createRow(2);
  3. Cell cell1_1 = row1.createCell(1);
  4. Cell cell1_2 = row1.createCell(2);
  5. Cell cell1_3 = row1.createCell(3);
  6. Cell cell2_3 = row2.createCell(3);
  7. cell1_1.setCellValue(30);
  8. cell1_2.setCellValue(25);
  9. cell1_3.setCellFormula("B2+C2");
  10. cell2_3.setCellFormula("MOD(B2,C2)");
Row row1 = wb.getSheet("sheetname13").createRow(1);
Row row2 = wb.getSheet("sheetname13").createRow(2);

Cell cell1_1 = row1.createCell(1);
Cell cell1_2 = row1.createCell(2);
Cell cell1_3 = row1.createCell(3);
Cell cell2_3 = row2.createCell(3);

cell1_1.setCellValue(30);
cell1_2.setCellValue(25);
cell1_3.setCellFormula("B2+C2");
cell2_3.setCellFormula("MOD(B2,C2)");




38、画直线,圆圈(椭圆),正方形(长方形),Textbox

 
Java代码 复制代码 收藏代码
  1. HSSFPatriarch patriarch = ((HSSFSheet)wb.getSheet("sheetname14")).createDrawingPatriarch();
  2. //直线
  3. HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 0, 0, 0,
  4. (short) 4, 2, (short) 6, 5);
  5. HSSFSimpleShape shape1 = patriarch.createSimpleShape(clientAnchor1);
  6. shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
  7. //圆圈(椭圆)
  8. HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 0, 0, 0,
  9. (short) 8, 4, (short) 6, 5);
  10. HSSFSimpleShape shape2 = patriarch.createSimpleShape(clientAnchor2);
  11. shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
  12. //正方形(长方形)
  13. HSSFClientAnchor clientAnchor3 = new HSSFClientAnchor(0, 0, 0, 0,
  14. (short) 12, 6, (short) 6, 5);
  15. HSSFSimpleShape shape3 = patriarch.createSimpleShape(clientAnchor3);
  16. shape3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
  17. //Textbox
  18. HSSFClientAnchor clientAnchor4 = new HSSFClientAnchor(0, 0, 0, 0,
  19. (short) 14, 8, (short) 6, 5);
  20. HSSFTextbox textbox = patriarch.createTextbox(clientAnchor4);
  21. textbox.setString(new HSSFRichTextString("This is a test"));
HSSFPatriarch patriarch = ((HSSFSheet)wb.getSheet("sheetname14")).createDrawingPatriarch();

//直线
HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 0, 0, 0,
		(short) 4, 2, (short) 6, 5);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(clientAnchor1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);

//圆圈(椭圆)
HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 0, 0, 0,
		(short) 8, 4, (short) 6, 5);
HSSFSimpleShape shape2 = patriarch.createSimpleShape(clientAnchor2);
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);

//正方形(长方形)
HSSFClientAnchor clientAnchor3 = new HSSFClientAnchor(0, 0, 0, 0,
		(short) 12, 6, (short) 6, 5);
HSSFSimpleShape shape3 = patriarch.createSimpleShape(clientAnchor3);
shape3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);

//Textbox
HSSFClientAnchor clientAnchor4 = new HSSFClientAnchor(0, 0, 0, 0,
		(short) 14, 8, (short) 6, 5);
HSSFTextbox textbox = patriarch.createTextbox(clientAnchor4);
textbox.setString(new HSSFRichTextString("This is a test"));



39、插入图片

Java代码 复制代码 收藏代码
  1. //需要commons-codec-1.6.jar
  2. FileInputStream jpeg = new FileInputStream("resource/test.jpg");
  3. byte[] bytes = IOUtils.toByteArray(jpeg);
  4. int pictureIndex = wb.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
  5. jpeg.close();
  6. HSSFCreationHelper helper = (HSSFCreationHelper) wb.getCreationHelper();
  7. HSSFPatriarch patriarch = ((HSSFSheet)wb.getSheet("sheetname15")).createDrawingPatriarch();
  8. HSSFClientAnchor clientAnchor = helper.createClientAnchor();
  9. clientAnchor.setCol1(3);
  10. clientAnchor.setRow1(2);
  11. HSSFPicture picture = patriarch.createPicture(clientAnchor, pictureIndex);
  12. picture.resize();
//需要commons-codec-1.6.jar
FileInputStream jpeg = new FileInputStream("resource/test.jpg");
byte[] bytes = IOUtils.toByteArray(jpeg);
int pictureIndex = wb.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
jpeg.close();

HSSFCreationHelper helper = (HSSFCreationHelper) wb.getCreationHelper();

HSSFPatriarch patriarch = ((HSSFSheet)wb.getSheet("sheetname15")).createDrawingPatriarch();

HSSFClientAnchor clientAnchor = helper.createClientAnchor();

clientAnchor.setCol1(3);
clientAnchor.setRow1(2);

HSSFPicture picture = patriarch.createPicture(clientAnchor, pictureIndex);
picture.resize();


java 读取 excel_第17张图片

40、设置可输入List

 
Java代码 复制代码 收藏代码
  1. CellRangeAddressList addressList = new CellRangeAddressList(
  2. 0,
  3. 0,
  4. 0,
  5. 0);
  6. final String[] DATA_LIST = new String[] {
  7. "10",
  8. "20",
  9. "30",
  10. };
  11. DVConstraint dvConstraint =
  12. DVConstraint.createExplicitListConstraint(DATA_LIST);
  13. HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
  14. dataValidation.setSuppressDropDownArrow(false);
  15. wb.getSheet("sheetname16").addValidationData(dataValidation);
CellRangeAddressList addressList = new CellRangeAddressList(
        0,
        0,
        0,
        0);

final String[] DATA_LIST = new String[] {
        "10",
        "20",
        "30",
};
DVConstraint dvConstraint =
    DVConstraint.createExplicitListConstraint(DATA_LIST);

HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);

wb.getSheet("sheetname16").addValidationData(dataValidation);




41、设置输入提示信息

Java代码 复制代码 收藏代码
  1. CellRangeAddressList addressList = new CellRangeAddressList(
  2. 0,
  3. 0,
  4. 0,
  5. 0);
  6. final String[] DATA_LIST = new String[] {
  7. "10",
  8. "20",
  9. "30",
  10. };
  11. DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(DATA_LIST);
  12. HSSFDataValidation dataValidation =
  13. new HSSFDataValidation(addressList, dvConstraint);
  14. dataValidation.setSuppressDropDownArrow(false);
  15. dataValidation.createPromptBox("输入提示", "请从下拉列表中选择!");
  16. dataValidation.setShowPromptBox(true);
  17. wb.getSheet("sheetname17").addValidationData(dataValidation);
CellRangeAddressList addressList = new CellRangeAddressList(
        0,
        0,
        0,
        0);

final String[] DATA_LIST = new String[] {
        "10",
        "20",
        "30",
};
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(DATA_LIST);

HSSFDataValidation dataValidation =
    new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
dataValidation.createPromptBox("输入提示", "请从下拉列表中选择!");
dataValidation.setShowPromptBox(true);

wb.getSheet("sheetname17").addValidationData(dataValidation);


java 读取 excel_第18张图片

你可能感兴趣的:(Excel)