JavaWorkBook对Excel的基本操作
1、异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
  解决⽅法:使⽤的poi的相关jar包⼀定版本⼀定要相同
2、maven所使⽤jar包,没有使⽤maven的话,就⽤poi-3.9.jar和poi-ooxml-3.9.jar(这个主要是⽤于Excel2007以后的版本)两个jar 包就⾏()
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
3、java导⼊Excel
  先上传Excel
//上传Excel
@RequestMapping("/uploadExcel")
public boolean uploadExcel(@RequestParam MultipartFile file,HttpServletRequest request) throws IOException {
if(!file.isEmpty()){
String filePath = OriginalFilename();
//windows
String savePath = Session().getServletContext().getRealPath(filePath);
//linux
//String savePath = "/home/odcuser/webapps/file";
File targetFile = new File(savePath);
if(!ists()){
targetFile.mkdirs();
}
return true;
}
return false;
}
在读取Excel⾥⾯的内容
public static void readExcel() throws Exception{
InputStream is = new FileInputStream(new File(fileName));
Workbook hssfWorkbook = null;
if (dsWith("xlsx")){
hssfWorkbook = new XSSFWorkbook(is);//Excel 2007
}else if (dsWith("xls")){
hssfWorkbook = new HSSFWorkbook(is);//Excel 2003
}
// HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
/
/ XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is);
User student = null;
List<User> list = new ArrayList<User>();
// 循环⼯作表Sheet
for (int numSheet = 0; numSheet &NumberOfSheets(); numSheet++) {  //HSSFSheet hssfSheet = SheetAt(numSheet);
Sheet hssfSheet = SheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环⾏Row
for (int rowNum = 1; rowNum <= LastRowNum(); rowNum++) {
//HSSFRow hssfRow = Row(rowNum);
Row hssfRow = Row(rowNum);
if (hssfRow != null) {
student = new User();
//HSSFCell name = Cell(0);
//HSSFCell pwd = Cell(1);
Cell name = Cell(0);
Cell pwd = Cell(1);
//这⾥是⾃⼰的逻辑
student.String());
student.String());
list.add(student);
}
}
}
}
4、导出Excel
//创建Excel
@RequestMapping("/createExcel")
public String createExcel(HttpServletResponse response) throws IOException {
//创建HSSFWorkbook对象(excel的⽂档对象)
HSSFWorkbook wb = new HSSFWorkbook();
/
/建⽴新的sheet对象(excel的表单)
HSSFSheet ateSheet("成绩表");
//在sheet⾥创建第⼀⾏,参数为⾏索引(excel的⾏),可以是0~65535之间的任何⼀个
HSSFRow ateRow(0);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何⼀个
HSSFCell ateCell(0);
//设置单元格内容
cell.setCellValue("学员考试成绩⼀览表");
//合并单元格CellRangeAddress构造参数依次表⽰起始⾏,截⾄⾏,起始列,截⾄列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//在sheet⾥创建第⼆⾏mkdirs方法
HSSFRow ateRow(1);
//创建单元格并设置单元格内容
//在sheet⾥创建第三⾏
HSSFRow ateRow(2);
//.....省略部分代码
//输出Excel⽂件
OutputStream OutputStream();
response.setHeader("Content-disposition", "attachment; filename=details.xls");
response.setContentType("application/msexcel");
wb.write(output);
output.close();
return null;
}
补充说明乱码问题
  1、⽂件名乱码(我发现只要解决了⽂件名乱码,其他乱码也会跟着解决)response.setHeader("Content-disposition", "attachment; filename=中⽂.xls");
  这个⽅法可以当做⼀个公⽤⽅法来使⽤,以后有乱码的都可以调⽤此⽅法
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = String(c).getBytes("utf-8");}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + HexString(k).toUpperCase());
}
}
}
String();
}
调⽤的时候,response.setHeader("Content-disposition", "attachment; filename="+toUtf8String("中⽂.xls"));
 我上⽹查的时候,⽹上是说
 今天要说的是在创建⼯作表时,⽤中⽂做⽂件名和⼯作表名会出现乱码的问题,先说以中⽂作为⼯作表名,⼤家创建⼯作表的代码⼀般如下:
 HSSFWorkbook workbook = new HSSFWorkbook();//创建EXCEL⽂件
HSSFSheet  sheet= ateSheet(sheetName);    //创建⼯作表
这样在⽤英⽂名作为⼯作表名是没问题的,但如果sheetName是中⽂字符,就会出现乱码,解决的⽅法如下代码:
HSSFSheet  sheet= ateSheet();
workbook.setSheetName(0, sheetName,(short)1); //这⾥(short)1是解决中⽂乱码的关键;⽽第⼀个参数是⼯作表的索引号。      但是我发现根本没有这个⽅法,只需要改了⽂件名的乱码,其他乱码⾃然就解决了

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。