A. 下载了个apache poi-3.6,怎样使用啊
这个。。。 涉及的内容太多,打字说清楚的话可以够一个小手册了。把poi-3.6-20091214.jar这个jar包解压缩拿出来,在eclipse的工程文件里作为外部jar包添加在jar包列表中,然后就可以创建一个类执行了。
B. org.apache.poi是什么jar包
apache poi各jar包介绍poi-3.12-20150511.jar (excel文件生成需要)poi-examples-3.12-20150511.jar(例子,开发不需要) poi-excelant-3.12-20150511.jar(不需要)poi-ooxml-3.12-20150511.jar(excel,word,ppt均需要)poi-ooxml-schemas-3.12-20150511.jar(excel需要)poi-scratchpad-3.12-20150511.jar(ppt,doc,vsd等需要)
C. 谁能给我一个详细的java通过Apache POI导出Excel方法,最好能给完整代码
package com.test;/** * 需要的jar包: * poi-3.0.2-FINAL-20080204.jar * poi-contrib-3.0.2-FINAL-20080204.jar *poi-scratchpad-3.0.2-FINAL-20080204.jar * poi-3.5-beta6-20090622.jar * geronimo-stax-api_1.0_spec-1.0.jar * ooxml-schemas-1.0.jar * openxml4j-bin-beta.jar * poi-ooxml-3.5-beta6-20090622.jar *xmlbeans-2.3.0.jar * dom4j-1.6.1.jar */import java.io.ByteArrayInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.StringWriter;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.util.PDFTextStripper;import org.apache.poi.POIOLE2TextExtractor;import org.apache.poi.POITextExtractor;import org.apache.poi.POIXMLDocument;import org.apache.poi.POIXMLTextExtractor;import org.apache.poi.extractor.ExtractorFactory;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hwpf.extractor.WordExtractor;import org.apache.poi.openxml4j.exceptions.OpenXML4JException;import org.apache.poi.openxml4j.opc.OPCPackage;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.apache.poi.xwpf.extractor.XWPFWordExtractor;import org.apache.xmlbeans.XmlException;public class WordAndExcelExtractor { public static void main(String[] args) { try { // 读取word String wordFile = "D:/1.doc"; //String wordText2007 = WordAndExcelExtractor.extractTextFromDOC2007(wordFile); //System.out.println("wordText2007=======" + wordText2007); InputStream isword = new FileInputStream(wordFile); WordExtractor wordExtractor = new WordExtractor(isword); System.out.println("word========" + wordExtractor.getText()); // 读取 Excel InputStream is = new FileInputStream("D:/测试.xls"); String excelText = WordAndExcelExtractor.extractTextFromXLS(is); System.out.println("text2003==========" + excelText); String excelFile = "D:/test2.xlsx"; String excelText2007 = WordAndExcelExtractor .extractTextFromXLS2007(excelFile); System.out.println("excelText2007==========" + excelText2007); // 读取 PPT PowerPointExtractor ppe = new PowerPointExtractor("D:/test.ppt"); System.out.println("ppt2003===============" + ppe.getText()); // System.out.println("###############################"); // System.out.println(ppe.getText(true, true, true, true)); // // InputStream is = new FileInputStream("D:/test.ppt"); // PowerPointExtractor ppt2003 = new PowerPointExtractor(is); // System.out.println(ppt2003.getText()); System.out.println("************************************"); XSLFPowerPointExtractor ppt = new XSLFPowerPointExtractor( POIXMLDocument.openPackage("D:/test2.pptx")); System.out.println("ppt2007=============================" + ppt.getText()); /* 读取PDF */ InputStream in = new FileInputStream("D:/test.pdf"); PDDocument pdfDocument = PDDocument.load(in); if (pdfDocument.isEncrypted()) { // 仅仅尝试使用默认密码打开加密的PDF pdfDocument.decrypt(""); } PDFTextStripper stripper = null; // 创建一个writer用来作来存储文件正文 StringWriter writer = new StringWriter(); if (stripper == null) { stripper = new PDFTextStripper(); } else { stripper.resetEngine(); } stripper.writeText(pdfDocument, writer); String contents = writer.getBuffer().toString(); System.out.println("pdfd===" + contents); /* 向Word中写入数据 */ byte[] a = contents.getBytes(); ByteArrayInputStream bs = new ByteArrayInputStream(a); POIFSFileSystem fs = new POIFSFileSystem(); // ///////////////////////////////// DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bs); // 以上两句代码不能省略,否则输出的是乱码 FileOutputStream fos = new FileOutputStream("D:\\dd.doc"); fs.writeFilesystem(fos); bs.close(); fos.flush(); fos.close(); System.out.println("写入成功"); } catch (Exception e) { e.printStackTrace(); } } /** * @Method: extractTextFromXLS * @Description: 从excel 2003档中提取纯文本 * * @param * @return String * @throws */ @SuppressWarnings("deprecation") private static String extractTextFromXLS(InputStream is) throws IOException { StringBuffer content = new StringBuffer(); HSSFWorkbook workbook = new HSSFWorkbook(is); // 创建对Excel工作簿文件的引用 for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) { if (null != workbook.getSheetAt(numSheets)) { HSSFSheet aSheet = workbook.getSheetAt(numSheets); // 获得一个sheet content.append(aSheet.getSheetName()); content.append("\r\n———————–\r\n"); for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet .getLastRowNum(); rowNumOfSheet++) { if (null != aSheet.getRow(rowNumOfSheet)) { HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一行 for (short cellNumOfRow = 0; cellNumOfRow <= aRow .getLastCellNum(); cellNumOfRow++) { if (null != aRow.getCell(cellNumOfRow)) { HSSFCell aCell = aRow.getCell(cellNumOfRow); // 获得列值 if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { content.append(aCell.getNumericCellValue()); } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { content.append(aCell.getBooleanCellValue()); } else { content.append(aCell.getStringCellValue()); } content.append("\t"); } } content.append("\r\n"); } } } } return content.toString(); } /** * @Method: extractTextFromXLS2007 * @Description: 从excel 2007文档中提取纯文本 * * @param * @return String * @throws */ private static String extractTextFromXLS2007(String fileName) throws Exception { StringBuffer content = new StringBuffer(); // 构造 XSSFWorkbook 对象,strPath 传入文件路径 XSSFWorkbook xwb = new XSSFWorkbook(fileName); // 循环工作表Sheet for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) { XSSFSheet xSheet = xwb.getSheetAt(numSheet); if (xSheet == null) { continue; } // 循环行Row for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) { XSSFRow xRow = xSheet.getRow(rowNum); if (xRow == null) { continue; } // 循环列Cell for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) { XSSFCell xCell = xRow.getCell(cellNum); if (xCell == null) { continue; } if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { content.append(xCell.getBooleanCellValue()); } else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { content.append(xCell.getNumericCellValue()); } else { content.append(xCell.getStringCellValue()); } } } } return content.toString(); }} 这是POI jar包的下载地址,我下载的是3.9版本的http://poi.apache.org/download.html
D. org.apache.poi.jar这个包哪里有免费可以下载的
CSDN有下载的 ,你免费注册就有积分可以下载了 ,直接网络搜索org.apache.poi.jar就出来啦
E. org.apache.poi.hssf.util.cellrangeaddress;是哪个jar包
存在旧版本中import org.apache.poi.hssf.util.CellRangeAddress;<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.14</version></dependency>新版本中import org.apache.poi.ss.util.CellRangeAddress;<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency>
F. springmvc使用poi导出excel需要什么jar
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。这里的方法支持导出excel至项目所在服务器,或导出至客户端浏览器供用户下载,下面我把两个实例都放出来。1.下载所需POI的jar包,并导入项目。2.添加一个User类,用于存放用户实体,类中内容如下:1 package com.mvc.po; 2 3 public class User { 4 private int id; 5 private String name; 6 private String password; 7 private int age; 8 9 public User() {10 11 }12 13 public User(int id, String name, String password, int age) {14 this.id = id;15 this.name = name;16 this.password = password;17 this.age = age;18 }19 public int getId() {20 return id;21 }22 public void setId(int id) {23 this.id = id;24 }25 public String getName() {26 return name;27 }28 public void setName(String name) {29 this.name = name;30 }31 public String getPassword() {32 return password;33 }34 public void setPassword(String password) {35 this.password = password;36 }37 public int getAge() {38 return age;39 }40 public void setAge(int age) {41 this.age = age;42 }43 }3.添加一个UserController类,类中内容如下:1 package com.mvc.controller; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import javax.servlet.ServletOutputStream; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.stereotype.Controller;10 import org.springframework.beans.factory.annotation.Autowired;11 import org.springframework.web.bind.annotation.RequestMapping;12 import org.springframework.web.bind.annotation.ResponseBody;13 14 import com.mvc.po.User;15 import com.mvc.service.UserService;16 17 @Controller18 public class UserController {19 20 @Autowired21 private UserService userService;22 23 @RequestMapping("/export.do")24 public @ResponseBody String export(HttpServletResponse response){ 25 response.setContentType("application/binary;charset=UTF-8");26 try{27 ServletOutputStream out=response.getOutputStream();28 String fileName=new String(("UserInfo "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(),"UTF-8");29 response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");30 String[] titles = { "用户编号", "用户姓名", "用户密码", "用户年龄" }; 31 userService.export(titles, out);32 return "success";33 } catch(Exception e){34 e.printStackTrace();35 return "导出信息失败";36 }37 }38 }4.添加一个接口类UserService和实现类UserServiceImpl,类中内容如下:1 package com.mvc.service;2 3 import javax.servlet.ServletOutputStream;4 import com.mvc.po.User;5 6 public interface UserService {7 public void export(String[] titles, ServletOutputStream out);8 }1 package com.mvc.service.impl; 2 3 import java.text.SimpleDateFormat; 4 import java.util.List; 5 6 import javax.servlet.ServletOutputStream; 7 8 import com.mvc..UserDAO; 9 import com.mvc.po.User;10 import com.mvc.service.UserService;11 12 import org.apache.poi.hssf.usermodel.HSSFCell;13 import org.apache.poi.hssf.usermodel.HSSFCellStyle;14 import org.apache.poi.hssf.usermodel.HSSFRow;15 import org.apache.poi.hssf.usermodel.HSSFSheet;16 import org.apache.poi.hssf.usermodel.HSSFWorkbook;17 import org.springframework.beans.factory.annotation.Autowired;18 import org.springframework.stereotype.Service;19 20 @Service21 public class UserServiceImpl implements UserService {22 23 @Autowired24 private UserDAO userDAO;25 26 @Override27 public void export(String[] titles, ServletOutputStream out) { 28 try{29 // 第一步,创建一个workbook,对应一个Excel文件30 HSSFWorkbook workbook = new HSSFWorkbook();31 // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet32 HSSFSheet hssfSheet = workbook.createSheet("sheet1");33 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short34 HSSFRow hssfRow = hssfSheet.createRow(0);35 // 第四步,创建单元格,并设置值表头 设置表头居中36 HSSFCellStyle hssfCellStyle = workbook.createCellStyle();37 //居中样式38 hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);39 40 HSSFCell hssfCell = null;41 for (int i = 0; i < titles.length; i++) {42 hssfCell = hssfRow.createCell(i);//列索引从0开始43 hssfCell.setCellValue(titles[i]);//列名144 hssfCell.setCellStyle(hssfCellStyle);//列居中显示 45 }46 47 // 第五步,写入实体数据 48 List<User> users = userDAO.query(); 49 50 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");51 if(users != null && !users.isEmpty()){52 for (int i = 0; i < users.size(); i++) {53 hssfRow = hssfSheet.createRow(i+1); 54 User user = users.get(i);55 56 // 第六步,创建单元格,并设置值57 int userid = 0;58 if(user.getId() != 0){59 userid = user.getId();60 }61 hssfRow.createCell(0).setCellValue(userid);62 String username = "";63 if(user.getName() != null){64 username = user.getName();65 }66 hssfRow.createCell(1).setCellValue(username);67 String password = "";68 if(user.getPassword() != null){69 password = user.getPassword();70 }71 hssfRow.createCell(2).setCellValue(password);72 int age = 0;73 if(user.getAge() != 0){74 age = user.getAge();75 }76 hssfRow.createCell(3).setCellValue(age);77 }78 }79 80 // 第七步,将文件输出到客户端浏览器81 try {82 workbook.write(out);83 out.flush();84 out.close();85 86 } catch (Exception e) {87 e.printStackTrace();88 }89 }catch(Exception e){90 e.printStackTrace();91 throw new Exception("导出信息失败!");92 } 93 }94 }5.添加一个接口类UserDAO和实现类UserDAOImpl,类中内容如下:1 package com.mvc.;2 3 import java.util.List;4 import com.mvc.po.User;5 6 public interface UserDAO {7 List<User> query(); 8 }1 package com.mvc..impl; 2 3 import java.util.List; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 import com.mvc..UserDAO; 8 import com.mvc.po.User; 9 10 import org.springframework.stereotype.Repository;11 import org.springframework.beans.factory.annotation.Autowired;12 import org.springframework.jdbc.core.JdbcTemplate;13 import org.springframework.jdbc.core.RowMapper;14 15 @Repository16 public class UserDAOImpl implements UserDAO {17 18 @Autowired19 private JdbcTemplate jdbcTemplate;20 21 public List<User> query() {22 return this.jdbcTemplate.query("select * from student",23 new RowMapper<User>() {24 public User mapRow(ResultSet rs, int arg1)25 throws SQLException {26 return new User(rs.getInt("sId"),27 rs.getString("sName"), rs.getString("sPwd"), rs28 .getInt("sAge"));29 }30 });31 }32 }这样就完成了excel导出至客户端浏览器,当然有时候也会用到导出excel至服务器上。只需要对本文步骤4中的第七步文件输出方式进行修改,如下:1 // 第七步,将文件存到指定位置2 try {3 FileOutputStream fileOutputStream = new FileOutputStream("C:/user.xls");//指定路径与名字和格式4 workbook.write(fileOutputStream);//讲数据写出去5 fileOutputStream.close();//关闭输出流6 } catch (Exception e) {7 e.printStackTrace();8 }然后去除controller类中的out参数设置就ok了。也可以看出其实两种方式只是最终保存方式不同,其他步骤是共通的。
G. 使用poi包读写excel需要导入哪几个jar包
<!– poi数据表格生成 –><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency>这一个就内够容了
H. Apache POI 有org.apache.poi.xssf包的那个版本,谁有 重赏100分
http://poi.apache.org/download.html下载poi-bin-3.9-20121203.zip解压后poi-ooxml-3.9-20121203.jar这个jar文件中有org.apache.poi.xssf包