728x90
반응형
■ apache poi 라이브러리를 이용하여 워드 문서(doc) 서식있는 텍스트파일(rtf)을 만드는 예제는 다음과 같습니다.
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText
public class PoOnlineDocumentumFileController{
public void poOnlinedownloadautofile() throws Exception{
//임시경로에 파일 생성
String TEMPPATH = "C:/" + File.separator + "Auto";
TEMPPATH = TEMPPATH + File.separator + String.valueOf(System.currentTimeMillis());
FileUtil.ensureDirectory(TEMPPATH);
File workingDir = new File(TEMPPATH);
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run.setText("This is a text paragraph having");
//하이퍼링크
XWPFParagraph hyperParagraph = document.createParagraph();
hyperParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFHyperlinkRun hyperlinkrun = createHyperlinkRun(hyperParagraph, "http://www.google.com");
hyperlinkrun.setText("a link to Google");
hyperlinkrun.setColor("0000FF");
hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);
//header/footer 생성
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
//write header content
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "This is header";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
//write footer content
CTP ctpFooter = CTP.Factory.newInstance();
CTR ctrFooter = ctpFooter.addNewR();
CTText ctFooter = ctrFooter.addNewT();
String footerText = "This is footer";
ctFooter.setStringValue(footerText);
XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
XWPFParagraph[] parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerParagraph;
policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
//Table 생성
XWPFTable table = document.createTable();
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
for (int a = 0; a < 2; a++) {
XWPFTableRow tableRowTwo = table.createRow();
for (int b = 0; b < 3; b++) {
tableRowTwo.getCell(b).setText("col one, row two");
}
}
//들여쓰기
XWPFParagraph paragraphText = document.createParagraph();
String textSample = "들여쓰기 \r\n 들여쓰기";
String[] values = textSample.split("\r\n");
for (int i = 0; i < values.length; i++) {
XWPFRun runText = paragraphText.insertNewRun(i);
runText.setFontFamily("Bookman Old Style");
runText.setFontSize(12);
runText.addBreak();
runText.addTab();
runText.setText(values[i]);
}
//파일쓰기
File projectCiFile = new File(workingDir, "파일명.rtf");
FileOutputStream out = new FileOutputStream(projectCiFile);
document.write(out);
out.close();
//브라우저에서 파일다운로드 할수있도록 처리
FileInputStream fis = null;
BufferedInputStream bin = null;
BufferedOutputStream bos = null;
try {
String fileName = "파일명.rtf";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(projectCiFile.getName(), "UTF-8") + ";");
response.setHeader("Content-Length", String.valueOf(projectCiFile.length()));
fis = new FileInputStream(projectCiFile);
bin = new BufferedInputStream(fis);
bos = new BufferedOutputStream(response.getOutputStream());
byte buf[] = new byte[2048];
for(int read = 0; (read = bin.read(buf)) != -1;) {
bos.write(buf, 0, read);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if (bos != null) bos.close();
if (bin != null) bin.close();
if (fis != null) fis.close();
try {
FileUtil.deleteDirectory(TEMPPATH);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//하이퍼링크만들기
static XWPFHyperlinkRun createHyperlinkRun(XWPFParagraph paragraph, String uri) {
String rId = paragraph.getDocument().getPackagePart().addExternalRelationship(
uri,
XWPFRelation.HYPERLINK.getRelation()
).getId();
CTHyperlink cthyperLink=paragraph.getCTP().addNewHyperlink();
cthyperLink.setId(rId);
cthyperLink.addNewR();
return new XWPFHyperlinkRun(
cthyperLink,
cthyperLink.getRArray(0),
paragraph);
}
}
파일 생성 결과
728x90
반응형
'JAVA' 카테고리의 다른 글
아파치 POI RTF 번호매기기 (0) | 2021.11.24 |
---|---|
이클립스 블록 선택 모드(열 선택) (0) | 2021.11.08 |
pd4ml 라이브러리 License 적용 (0) | 2021.11.03 |
Tomcat-Hot Deploy설정 방법 톰캣 재실행 없이 Class 변경 (0) | 2021.07.16 |
pd4ml을 이용하여 PDF 생성하기 (0) | 2021.06.29 |
댓글