Skip to content

Instantly share code, notes, and snippets.

@joker1007
Created August 27, 2010 10:14
Show Gist options
  • Save joker1007/553145 to your computer and use it in GitHub Desktop.
Save joker1007/553145 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.xmlbeans.XmlException;
public class TargetFile {
private String filename;
private String filepath;
private long size;
private long mtime;
private String contentText;
private File fileObj;
public void setFilename(String filename) {
this.filename = filename;
}
public String getFilename() {
return filename;
}
public void setSize(long size) {
this.size = size;
}
public long getSize() {
return size;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
public String getFilepath() {
return filepath;
}
public void setContentText(String contentText) {
this.contentText = contentText;
}
public String getContentText() {
return contentText;
}
public void setMtime(long mtime) {
this.mtime = mtime;
}
public long getMtime() {
return mtime;
}
TargetFile() {
filename = null;
filepath = null;
size = 0;
mtime = 0;
contentText = null;
fileObj = null;
}
TargetFile(File file) {
filename = file.getName();
filepath = file.getAbsolutePath();
size = file.length();
mtime = file.lastModified();
contentText = null;
fileObj = file;
}
public void getFileContent() {
String regexp = ".*\\.(xls|xlsx|xlsm|doc|docx)$";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(filename);
if (matcher.find()) {
excelExtract();
}
}
private void excelExtract() {
POITextExtractor extractor;
try {
extractor = ExtractorFactory.createExtractor(fileObj);
contentText = extractor.getText();
} catch (InvalidFormatException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} catch (OpenXML4JException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} catch (XmlException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment