JAVA读取word,excel内容
2010年5月6日
没有评论
上次介绍了用java读取rtf文件格式的内容,那用java语言怎么来读取word文档以及excel中的内容,这些在编程的时候是经常会用到的,java读取word文档及Excel的内容在java的API中没有是没有现成的类或方法的,所以我们要用到一个控件——POI控件:POI控件的下载及配置
下面就是关于java读取word和Excel内容的代码: 阅读全文…
上次介绍了用java读取rtf文件格式的内容,那用java语言怎么来读取word文档以及excel中的内容,这些在编程的时候是经常会用到的,java读取word文档及Excel的内容在java的API中没有是没有现成的类或方法的,所以我们要用到一个控件——POI控件:POI控件的下载及配置
下面就是关于java读取word和Excel内容的代码: 阅读全文…
java中读取rtf文件格式的内容有事在项目中用到,java API中有相关的类和方法用来读取rtf文件格式的内容,这样利用相关的类和方法就能获取到我们需要的内容,不需要什么插件了呵呵,下面是代码,特记录下:
package textReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
public class RtfReader {
public RtfReader(){
}
/**
* @param filePath 文件路径
* @return 读出的rtf的内容
*/
public String getTextFromRtf(String filePath) {
String result = null;
File file = new File(filePath);
RTFEditorKit rtf = new RTFEditorKit().
try {
DefaultStyledDocument styledDoc = new DefaultStyledDocument();
InputStream is = new FileInputStream(file);
rtf.read(is, styledDoc, 0);
result = new String(styledDoc.getText(0,styledDoc.getLength()).getBytes("ISO8859_1"));
//提取文本,读取中文需要使用ISO8859_1编码,否则会出现乱码
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
return result;
}
}
在java编程中,对于时间的比较总是很多的,下面列出了常用的java时间比较的方法:
import java.text.*;
import java.util.*;
public class TimeCompare{
public static void main(String[] args){
boolean flag = isDateBefore(\"2004-09-09 12:12:12\",\"2005-09-09 16:00:00\");
System.out.println(flag);
flag = isDateBefore(\"2006-09-09 01:01:01\",\"2005-09-09 16:00:00\");
System.out.println(flag);
flag = isDateBefore(\"2005-09-09 01:01:01\");
System.out.println(flag);
}
//判断时间date1是否在时间date2之前
//时间格式 2005-4-21 16:16:34
public static boolean isDateBefore(String date1,String date2){
try{
DateFormat df = DateFormat.getDateTimeInstance();
return df.parse(date1).before(df.parse(date2));
}catch(ParseException e){
System.out.print(\"[SYS] \" + e.getMessage());
return false;
}
}
//判断当前时间是否在时间date2之前
//时间格式 2005-4-21 16:16:34
public static boolean isDateBefore(String date2){
try{
Date date1 = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
return date1.before(df.parse(date2));
}catch(ParseException e){
System.out.print(\"[SYS] \" + e.getMessage());
return false;
}
}
}
看到这里就知道怎么来判断比较时间格式为2005-4-21的了;下面是我仿照写的 一个静态方法,年前在工作中正好用到,呵呵!
//判断当前时间是否在时间date2之后或相等
//时间格式 2005-4-21
public static boolean isDateAfterOrEqual(String date2){
boolean flag=false;
try{
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = new Date();
if(fmt.parse(fmt.format(date1)).equals(fmt.parse(date2))||fmt.parse(fmt.format(date1)).after(fmt.parse(date2)))
flag=true;
}catch(ParseException e){
System.out.print("[SYS] " + e.getMessage());
flag = false;
}
return flag;
}
Recent Comments