java 怎么读取服务器上的word文件中的内容
通过流来读取,例如:TextFileForm fileForm = (TextFileForm) form; FormFile formFile = fileForm.getTxtFile(); if (formFile.getFileData().length == 0) { response.setCharacterEncoding("gb2312"); response.getWriter().write(""); } InputStream in = formFile.getInputStream(); WordExtractor extractor = new WordExtractor(); String str = extractor.extractText(in);这段代码就是负责读取word的
怎么用Java读取word文档里的内容格式信息,比如标题字体,颜色和段...
OFFICE文档使用POI控件,PDF可以使用PDFBOX0.7.3控件,完全支持中文,用XPDF也行,不过感觉PDFBOX比较好,而且作者也在更新。
水平有限,万望各位指正WORD:import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.poi.hwpf.extractor.WordExtractor;import java.io.File;import java.io.InputStream;import java.io.FileInputStream;import com.search.code.Index;public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {String bodyText = null;try {WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream bodyText = ex.getText();if(!bodyText.equals("")){index.AddIndex(url, title, bodyText);}}catch (DocCenterException e) {throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);}catch(Exception e){e.printStackTrace();}}return null;}Excel:import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.poi.hwpf.extractor.WordExtractor;import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.File;import java.io.InputStream;import java.io.FileInputStream;import com.search.code.Index;public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {StringBuffer content = new StringBuffer();try{HSSFWorkbook workbook = new HSSFWorkbook(is);//创建对Excel工作簿文件的引用 for (int numSheets = 0; numSheets这样可以么?
java如何分页读取Word的内容?
1.下载下载3.8beta4版本,请记得一定要下载该版本,其他版本读取word模板并改写内容生成新的文件后,打开新文件时会提示“word无法读取文档,文档可能损坏。
”2.集成到项目这一步很简单,只要把下载后解压得到的poi-3.8-beta4-20110826.jar和poi-scratchpad-3.8-beta4-20110826.jar两个文件复制到java web项目的lib目录下就行了3.制作word模板把需要变动的值全部用代码来代替,例如你需要改变名称的值,则可以在模板中用name来表示。
详细见附件中的doc文件。
4.调用接口方法实现对word的读写操作整个过程就是先读取模板,然后修改内容,再重新生成新的文档保存到本地或者输出文件流提供下载,下面分别是生成新文档和输出文件流两种方式的代码片断,详细的代码请见下列代码中的readwriteWord()两个重载方法。
java中怎么实现读取word.doc文档分辨标题,文号,签发日期等信息
展开全部 /** ** 标题:readExcel * 描述:读取Excel文件数据 * @param excelfilePath Excel文件路径 * @param startRow 开始行 * @param startCol 开始列 * @return List> * @throws IOException * @throws BiffException */ public List> readExcel(String excelfilePath,int startRow, int startCol)throws IOException, BiffException { // 读取xls文件 InputStream ins = new FileInputStream(excelfilePath); // 设置读文件编码 WorkbookSettings setEncode = new WorkbookSettings(); setEncode.setEncoding("UTF-8"); Workbook rwb = Workbook.getWorkbook(ins, setEncode); List> alldata = new ArrayList>(); Map data = null; alldata.clear(); // 获得当前Excel表共有几个sheet Sheet[] sheets = rwb.getSheets(); // 获得表数 int pages = sheets.length; // 将excel表中的数据读取出来 // 在从Excel中读取数据的时候不需要知道每个sheet有几行,有那多少列 for (int i = 0; i (); // 行循环,Excel的行列是从(0,0)开始 for (int c = startCol; c < cols; c++) { Cell excelRows = sheet.getCell(c, r); data.put("bgbh", excelRows.getContents()); } alldata.add(data); }} ins.close(); return alldata; }
怎么样用java去读取一个excel表格并且把数据写入一个word
展开全部 public boolean sendTextMail(MailSenderInfo mailInfo) { // 判断是否需要身份认证 MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { // 如果需要身份认证,则创建一个密码验证器 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMailSession = null; //sendMailSession = Session.getDefaultInstance(pro,authenticator); //获取默认可能报错 sendMailSession = Session.getInstance(pro,authenticator);//新创建一个session if (sendMailSession==null){ System.out.println("无法获取邮件邮件Session"); } try { // 根据session创建一个邮件消息 Message mailMessage = new MimeMessage(sendMailSession); // 创建邮件发送者地址 Address from = new InternetAddress(mailInfo.getFromAddress()); // 设置邮件消息的发送者 mailMessage.setFrom(from); // 创建邮件的接收者地址,并设置到邮件消息中 Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO,to); // 设置邮件消息的主题 mailMessage.setSubject(mailInfo.getSubject()); // 设置邮件消息发送的时间 mailMessage.setSentDate(new Date()); // 设置邮件消息的主要内容 String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); //添加附件// if(mailInfo.getAttachFileNames()!=null || mailInfo.getAttachFileNames().length>0){// Multipart mp = new MimeMultipart(); // MimeBodyPart mbp=null;// for(String fileName:mailInfo.getAttachFileNames()){// mbp=new MimeBodyPart();// FileDataSource fds=new FileDataSource(fileName); //得到数据源 // mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart // mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart // mp.addBodyPart(mbp); // }// mailMessage.setContent(mp);// } // 发送邮件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } public class MailSenderInfo { // 发送邮件的服务器的IP和端口 private String mailServerHost; private String mailServerPort = "25"; // 邮件发送者的地址 private String fromAddress; // 邮件接收者的地址 private String toAddress; // 登陆邮件发送服务器的用户名和密码 private String userName; private String password; // 是否需要身份验证 private boolean validate = false; // 邮件主题 private String subject; // 邮件的文本内容 private String content; // 邮件附件的文件名 private String[] attachFileNames; //邮件抄送人 private List ccUserList; /** *//** * 获得邮件会话属性 */ public Properties getProperties(){ Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); p.put("mail.smtp.auth", validate ? "true" : "false"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; } public List getCcUserList(){ return ccUserList; } public void setCcUserList(List ccUserList){ this.ccUserList = ccUserList; } } public static void main(String[] args) { // 这个类主要是设置邮件 MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.163.com"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("zhengzhanzong@...
转载请注明出处51数据库 » java 读取word中表格内容
鹏程缘