我建议您使用Apache POI文档。 我会使用文档和获取文本为MS-Excel工作表轻易格式化做一些实验..
http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html
我在浏览通过API当我看到DATAFORMAT类及其层次, BuiltinFormats类, 和CellStyle类的setDataFormat方法。 所以做了一些实验,下面的代码似乎工作!现在
XSSFCellStyle textFormatStyle = book.createCellStyle();
textFormatStyle.setDataFormat((short)BuiltinFormats.getBuiltinFormat("text"));
XSSFCell cell = row.createCell(columnIndex++);
cell.setCellStyle(textFormatStyle);
,一旦创建电子表格, 您可以编辑一个细胞,当你卡销, 格式仍然是“文本”。
我有你展示另一种方式与完整的例子.. 在我将进一步展示一个效果,你可以添加按照您的要求......
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Style example");
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("This is bold");
cell.setCellStyle(style);
font = workbook.createFont();
font.setItalic(true);
style = workbook.createCellStyle();
style.setFont(font);
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("This is italic");
cell.setCellStyle(style);
try {
FileOutputStream out = new FileOutputStream(new File("C:\\style.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
该代码会产生波纹管输出:
这也将成为为Excel虽然不是字! – Gagravarr
谢谢你的回复。虽然你给出了在Excel表格中定义样式的答案,但是需要获取和应用旧ms-office .doc文件中使用的相同样式。 我正在寻找一种方法来获取应用于文档文件中每个文本的样式。一旦我得到了样式,在创建doc文件的新副本(带有修改的内容)的同时应用样式将会很容易。 – nagesh
我很抱歉误导你。 我已经添加了格式化Ms-Word文档的新答案,请检查... –