java excel poi hssf_POI操作Excel详解,HSSF和XSSF两种方式

1 packagecom.tools.poi.lesson1;2

3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileNotFoundException;6 importjava.io.FileOutputStream;7 importjava.io.IOException;8 importjava.io.InputStream;9 importjava.io.OutputStream;10 importjava.text.ParseException;11 importjava.text.SimpleDateFormat;12 importjava.util.ArrayList;13 importjava.util.List;14

15 importorg.apache.poi.hssf.usermodel.HSSFCell;16 importorg.apache.poi.hssf.usermodel.HSSFCellStyle;17 importorg.apache.poi.hssf.usermodel.HSSFRow;18 importorg.apache.poi.hssf.usermodel.HSSFSheet;19 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;20 importorg.apache.poi.hssf.util.HSSFColor;21 importorg.apache.poi.openxml4j.exceptions.InvalidFormatException;22 importorg.apache.poi.poifs.filesystem.POIFSFileSystem;23 importorg.apache.poi.ss.usermodel.Cell;24 importorg.apache.poi.ss.usermodel.CellStyle;25 importorg.apache.poi.ss.usermodel.Row;26 importorg.apache.poi.ss.usermodel.Sheet;27 importorg.apache.poi.ss.usermodel.Workbook;28 importorg.apache.poi.ss.usermodel.WorkbookFactory;29 importorg.apache.poi.ss.util.WorkbookUtil;30

31 importcom.tools.poi.bean.Student;32

33 public classExcelUtilWithXSSF {34 public static voidmain(String[] args) {35 try{36 getExcelAsFile("d:/FTP/系统报表.xls");37 } catch(FileNotFoundException e) {38 e.printStackTrace();39 } catch(IOException e) {40 e.printStackTrace();41 } catch(InvalidFormatException e) {42 e.printStackTrace();43 }44

45

46 //try {47 //CreateExcelDemo1();48 //} catch (ParseException e) {49 //e.printStackTrace();50 //}

51

52

53 }54

55 /**

56 * 得到Excel,并解析内容 对2007及以上版本 使用XSSF解析57 *@paramfile58 *@throwsFileNotFoundException59 *@throwsIOException60 *@throwsInvalidFormatException61 */

62 public static void getExcelAsFile(String file) throwsFileNotFoundException, IOException, InvalidFormatException{63 // //1.得到Excel常用对象64 //POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));65 // //2.得到Excel工作簿对象66 //HSSFWorkbook wb = new HSSFWorkbook(fs);

67

68

69

70 InputStream ins = null;71 Workbook wb = null;72 ins=new FileInputStream(newFile(file));73 //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);

74 wb =WorkbookFactory.create(ins);75 ins.close();76

77

78 //3.得到Excel工作表对象

79 Sheet sheet = wb.getSheetAt(0);80 //总行数

81 int trLength =sheet.getLastRowNum();82 //4.得到Excel工作表的行

83 Row row = sheet.getRow(0);84 //总列数

85 int tdLength =row.getLastCellNum();86 //5.得到Excel工作表指定行的单元格

87 Cell cell = row.getCell((short)1);88 //6.得到单元格样式

89 CellStyle cellStyle =cell.getCellStyle();90

91 for(int i=5;i

93 Row row1 =sheet.getRow(i);94 for(int j=0;j

96 Cell cell1 =row1.getCell(j);97 /**

98 * 为了处理:Excel异常Cannot get a text value from a numeric cell99 * 将所有列中的内容都设置成String类型格式100 */

101 if(cell1!=null){102 cell1.setCellType(Cell.CELL_TYPE_STRING);103 }104

105 if(j==5&&i<=10){106 cell1.setCellValue("1000");107 }108

109 //获得每一列中的值

110 System.out.print(cell1+" ");111 }112 System.out.println();113 }114

115 //将修改后的数据保存

116 OutputStream out = newFileOutputStream(file);117 wb.write(out);118 }119

120

121 /**

122 * 创建Excel,并写入内容123 */

124 public static voidCreateExcel(){125

126 //1.创建Excel工作薄对象

127 HSSFWorkbook wb = newHSSFWorkbook();128 //2.创建Excel工作表对象

129 HSSFSheet sheet = wb.createSheet("new Sheet");130 //3.创建Excel工作表的行

131 HSSFRow row = sheet.createRow(6);132 //4.创建单元格样式

133 CellStyle cellStyle =wb.createCellStyle();134 //设置这些样式

135 cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);136 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);137 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);138 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);139 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);140 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);141 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);142

143

144

145 //5.创建Excel工作表指定行的单元格

146 row.createCell(0).setCellStyle(cellStyle);147 //6.设置Excel工作表的值

148 row.createCell(0).setCellValue("aaaa");149

150 row.createCell(1).setCellStyle(cellStyle);151 row.createCell(1).setCellValue("bbbb");152

153

154 //设置sheet名称和单元格内容

155 wb.setSheetName(0,"第一张工作表");156 //设置单元格内容 cell.setCellValue("单元格内容");157

158 //最后一步,将文件存到指定位置

159 try

160 {161 FileOutputStream fout = new FileOutputStream("E:/students.xls");162 wb.write(fout);163 fout.close();164 }165 catch(Exception e)166 {167 e.printStackTrace();168 }169 }170

171 /**

172 * 创建Excel的实例173 *@throwsParseException174 */

175 public static void CreateExcelDemo1() throwsParseException{176 List list = newArrayList();177 SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");178 Student user1 = new Student(1, "张三", 16,true, df.parse("1997-03-12"));179 Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));180 Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));181 list.add(user1);182 list.add(user2);183 list.add(user3);184

185

186 //第一步,创建一个webbook,对应一个Excel文件

187 HSSFWorkbook wb = newHSSFWorkbook();188 //第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

189 HSSFSheet sheet = wb.createSheet("学生表一");190 //第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

191 HSSFRow row = sheet.createRow((int) 0);192 //第四步,创建单元格,并设置值表头 设置表头居中

193 HSSFCellStyle style =wb.createCellStyle();194 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中格式

195

196 HSSFCell cell = row.createCell((short) 0);197 cell.setCellValue("学号");198 cell.setCellStyle(style);199 cell = row.createCell((short) 1);200 cell.setCellValue("姓名");201 cell.setCellStyle(style);202 cell = row.createCell((short) 2);203 cell.setCellValue("年龄");204 cell.setCellStyle(style);205 cell = row.createCell((short) 3);206 cell.setCellValue("性别");207 cell.setCellStyle(style);208 cell = row.createCell((short) 4);209 cell.setCellValue("生日");210 cell.setCellStyle(style);211

212 //第五步,写入实体数据 实际应用中这些数据从数据库得到,

213

214 for (int i = 0; i < list.size(); i++)215 {216 row = sheet.createRow((int) i + 1);217 Student stu =(Student) list.get(i);218 //第四步,创建单元格,并设置值

219 row.createCell((short) 0).setCellValue((double) stu.getId());220 row.createCell((short) 1).setCellValue(stu.getName());221 row.createCell((short) 2).setCellValue((double) stu.getAge());222 row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");223 cell = row.createCell((short) 4);224 cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu225 .getBirthday()));226 }227 //第六步,将文件存到指定位置

228 try

229 {230 FileOutputStream fout = new FileOutputStream("E:/students.xls");231 wb.write(fout);232 fout.close();233 }234 catch(Exception e)235 {236 e.printStackTrace();237 }238

239

240

241 }242 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值