java apache.poi_一脸懵逼学习Java操作Excel之POI(Apache POI)

1 package com.bie;2

3 import java.io.FileOutputStream;4 import java.io.IOException;5

6 import org.apache.poi.hssf.usermodel.HSSFCellStyle;7 import org.apache.poi.hssf.usermodel.HSSFRichTextString;8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;9 import org.apache.poi.ss.usermodel.Cell;10 import org.apache.poi.ss.usermodel.CellStyle;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 /**18 * @author Author:别先生19 * @date Date:2017年9月9日 上午10:14:3120 *21 *22 */

23 public classPoiTest {24

25 /***26 * 创建一个单元格并为其设定指定的对齐方式27 * @param wb 工作簿28 * @param row 行29 * @param column 列30 * @param halign 水平对齐的方式31 * @param valign 垂直对齐的方式32 */

33 private static void createCell(Workbook wb,Row row,short column,short halign,shortvalign){34 //创建单元格

35 Cell cell =row.createCell(column);36 //设置单元格的值

37 cell.setCellValue(new HSSFRichTextString("测试内容"));38 //创建单元格的样式

39 CellStyle cellStyle =wb.createCellStyle();40 //设置单元格水平对齐方式

41 cellStyle.setAlignment(halign);42 //设置单元格垂直对齐方式

43 cellStyle.setVerticalAlignment(valign);44

45 //设置边框线和颜色46 //底部边框

47 cellStyle.setBorderBottom(CellStyle.BORDER_THIN);48 //底部颜色

49 cellStyle.setBottomBorderColor(IndexedColors.BLUE.getIndex());50

51 //左边边框

52 cellStyle.setBorderLeft(CellStyle.BORDER_DASH_DOT);53 //左边颜色

54 cellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());55

56 //右边边框

57 cellStyle.setRightBorderColor(CellStyle.ALIGN_FILL);58 //右边边框颜色

59 cellStyle.setBottomBorderColor(IndexedColors.DARK_YELLOW.getIndex());60

61 //顶部边框

62 cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);63 //顶部颜色

64 cellStyle.setTopBorderColor(IndexedColors.AUTOMATIC.getIndex());65

66 //设置单元格填充色和颜色操作67 //设置背景颜色

68 cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());69 cellStyle.setFillPattern(CellStyle.BIG_SPOTS);70 //设置前景颜色

71 cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());72 cellStyle.setFillPattern(CellStyle.BIG_SPOTS);73

74 //设置单元格的样式

75 cell.setCellStyle(cellStyle);76 }77

78 public static voidmain(String[] args) throws IOException {79 //定义一个工作簿

80 Workbook wb =newHSSFWorkbook();81 //创建第一个sheet页

82 Sheet createSheet = wb.createSheet("第一个sheet");83 //创建第一行

84 Row createRow = createSheet.createRow(0);85 createRow.setHeightInPoints(30);86

87 //调用工具方法,创建单元格88 //单元格的对齐方式的调用和使用

89 createCell(wb, createRow, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);;90 createCell(wb, createRow, (short)1, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER);;91 createCell(wb, createRow, (short)2, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);;92 createCell(wb, createRow, (short)3, HSSFCellStyle.ALIGN_JUSTIFY, HSSFCellStyle.VERTICAL_JUSTIFY);;93

94

95 //创建单元格96 //创建第3行

97 Row createRow2 = createSheet.createRow(2);98 createRow.setHeightInPoints(30);99 //创建第一列和第二列

100 Cell createCell = createRow2.createCell(0);101 createCell.setCellValue("单元格合并1");102

103 Cell createCell2 = createRow2.createCell(1);104 createCell2.setCellValue("单元格合并2");105

106 //单元格合并,起始行,结束行,起始列,结束列

107 createSheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 3));108

109

110 //创建输出流

111 FileOutputStream fos = new FileOutputStream("d:\\poi.xlsx");112 //将内容写到excel文件中

113 wb.write(fos);114 //关闭流

115 fos.close();116 }117

118 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值