1. included ooxml javadocs in build.xml2. added a new rich example: BusinessPlan.java3. misc bug fixes
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@711839 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
178c3de886
commit
c4b1dc2e55
@ -1000,7 +1000,7 @@ FORREST_HOME environment variable!</echo>
|
|||||||
<packageset dir="${contrib.src}" defaultexcludes="yes">
|
<packageset dir="${contrib.src}" defaultexcludes="yes">
|
||||||
<include name="org/apache/poi/**"/>
|
<include name="org/apache/poi/**"/>
|
||||||
</packageset>
|
</packageset>
|
||||||
<packageset dir="${examples.src}" defaultexcludes="yes">
|
<packageset dir="${ooxml.src}" defaultexcludes="yes">
|
||||||
<include name="org/apache/poi/**"/>
|
<include name="org/apache/poi/**"/>
|
||||||
</packageset>
|
</packageset>
|
||||||
|
|
||||||
|
@ -1,178 +0,0 @@
|
|||||||
/* ====================================================================
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
||||||
contributor license agreements. See the NOTICE file distributed with
|
|
||||||
this work for additional information regarding copyright ownership.
|
|
||||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
||||||
(the "License"); you may not use this file except in compliance with
|
|
||||||
the License. You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
==================================================================== */
|
|
||||||
package org.apache.poi.ss.usermodel.examples;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
|
||||||
import org.apache.poi.ss.usermodel.CreationHelper;
|
|
||||||
import org.apache.poi.ss.usermodel.DateUtil;
|
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
|
||||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
||||||
import org.apache.poi.ss.util.CellReference;
|
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Various things from the quick guide documentation
|
|
||||||
*/
|
|
||||||
public class FromQuickGuide {
|
|
||||||
public static void newWorkbook() throws IOException {
|
|
||||||
boolean doHSSF = true;
|
|
||||||
boolean doXSSF = true;
|
|
||||||
|
|
||||||
if(doHSSF) {
|
|
||||||
Workbook wb = new HSSFWorkbook();
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
}
|
|
||||||
if(doXSSF) {
|
|
||||||
Workbook wb = new XSSFWorkbook();
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void newSheet() throws IOException {
|
|
||||||
Workbook[] wbs = new Workbook[] {
|
|
||||||
new HSSFWorkbook(), new XSSFWorkbook()
|
|
||||||
};
|
|
||||||
|
|
||||||
for (int i = 0; i < wbs.length; i++) {
|
|
||||||
Workbook wb = wbs[i];
|
|
||||||
Sheet sheet1 = wb.createSheet("new sheet");
|
|
||||||
Sheet sheet2 = wb.createSheet("second sheet");
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void newCells() throws IOException {
|
|
||||||
Workbook[] wbs = new Workbook[] {
|
|
||||||
new HSSFWorkbook(), new XSSFWorkbook()
|
|
||||||
};
|
|
||||||
|
|
||||||
for (int i = 0; i < wbs.length; i++) {
|
|
||||||
Workbook wb = wbs[i];
|
|
||||||
CreationHelper createHelper = wb.getCreationHelper();
|
|
||||||
Sheet sheet = wb.createSheet("new sheet");
|
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
|
||||||
Row row = sheet.createRow((short)0);
|
|
||||||
// Create a cell and put a value in it.
|
|
||||||
Cell cell = row.createCell((short)0);
|
|
||||||
cell.setCellValue(1);
|
|
||||||
|
|
||||||
// Or do it on one line.
|
|
||||||
row.createCell((short)1).setCellValue(1.2);
|
|
||||||
row.createCell((short)2).setCellValue(
|
|
||||||
createHelper.createRichTextString("This is a string"));
|
|
||||||
row.createCell((short)3).setCellValue(true);
|
|
||||||
|
|
||||||
// Write the output to a file
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void newDateCells() throws IOException {
|
|
||||||
Workbook wb = new HSSFWorkbook();
|
|
||||||
//Workbook wb = new XSSFWorkbook();
|
|
||||||
CreationHelper createHelper = wb.getCreationHelper();
|
|
||||||
Sheet sheet = wb.createSheet("new sheet");
|
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
|
||||||
Row row = sheet.createRow((short)0);
|
|
||||||
|
|
||||||
// Create a cell and put a date value in it. The first cell is not styled
|
|
||||||
// as a date.
|
|
||||||
Cell cell = row.createCell((short)0);
|
|
||||||
cell.setCellValue(new Date());
|
|
||||||
|
|
||||||
// we style the second cell as a date (and time). It is important to
|
|
||||||
// create a new cell style from the workbook otherwise you can end up
|
|
||||||
// modifying the built in style and effecting not only this cell but other cells.
|
|
||||||
CellStyle cellStyle = wb.createCellStyle();
|
|
||||||
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
|
|
||||||
cell = row.createCell((short)1);
|
|
||||||
cell.setCellValue(new Date());
|
|
||||||
cell.setCellStyle(cellStyle);
|
|
||||||
|
|
||||||
// Write the output to a file
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void iterating() {
|
|
||||||
Workbook wb = new HSSFWorkbook();
|
|
||||||
Sheet sheet = wb.createSheet("new sheet");
|
|
||||||
|
|
||||||
for (Row row : sheet) {
|
|
||||||
for (Cell cell : row) {
|
|
||||||
// Do something here
|
|
||||||
System.out.println(cell.getCellType());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void getCellContents(Sheet sheet) {
|
|
||||||
for (Row row : sheet) {
|
|
||||||
for (Cell cell : row) {
|
|
||||||
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
|
|
||||||
System.out.print(cellRef.formatAsString());
|
|
||||||
System.out.print(" - ");
|
|
||||||
|
|
||||||
switch(cell.getCellType()) {
|
|
||||||
case Cell.CELL_TYPE_STRING:
|
|
||||||
System.out.println(cell.getRichStringCellValue().getString());
|
|
||||||
break;
|
|
||||||
case Cell.CELL_TYPE_NUMERIC:
|
|
||||||
if(DateUtil.isCellDateFormatted(cell)) {
|
|
||||||
System.out.println(cell.getDateCellValue());
|
|
||||||
} else {
|
|
||||||
System.out.println(cell.getNumericCellValue());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Cell.CELL_TYPE_BOOLEAN:
|
|
||||||
System.out.println(cell.getBooleanCellValue());
|
|
||||||
break;
|
|
||||||
case Cell.CELL_TYPE_FORMULA:
|
|
||||||
System.out.println(cell.getCellFormula());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
Workbook wb = WorkbookFactory.create(new FileInputStream("src/testcases/org/apache/poi/hssf/data/WithMoreVariousData.xlsx"));
|
|
||||||
getCellContents(wb.getSheetAt(0));
|
|
||||||
}
|
|
||||||
}
|
|
@ -28,7 +28,7 @@ import java.io.FileOutputStream;
|
|||||||
public class AligningCells {
|
public class AligningCells {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
|
|
||||||
Sheet sheet = wb.createSheet();
|
Sheet sheet = wb.createSheet();
|
||||||
Row row = sheet.createRow((short) 2);
|
Row row = sheet.createRow((short) 2);
|
||||||
|
@ -128,7 +128,7 @@ public class CalendarDemo {
|
|||||||
/**
|
/**
|
||||||
* cell styles used for formatting calendar sheets
|
* cell styles used for formatting calendar sheets
|
||||||
*/
|
*/
|
||||||
public static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
|
private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
|
||||||
Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
|
Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
|
||||||
|
|
||||||
XSSFCellStyle style;
|
XSSFCellStyle style;
|
||||||
|
@ -31,7 +31,7 @@ public class CreateCell {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
CreationHelper creationHelper = wb.getCreationHelper();
|
CreationHelper creationHelper = wb.getCreationHelper();
|
||||||
Sheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ public class CreateUserDefinedDataFormats {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("format sheet");
|
Sheet sheet = wb.createSheet("format sheet");
|
||||||
CellStyle style;
|
CellStyle style;
|
||||||
DataFormat format = wb.createDataFormat();
|
DataFormat format = wb.createDataFormat();
|
||||||
|
@ -28,7 +28,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||||||
*/
|
*/
|
||||||
public class FillsAndColors {
|
public class FillsAndColors {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
// Create a row and put some cells in it. Rows are 0 based.
|
||||||
|
@ -27,7 +27,7 @@ public class FitSheetToOnePage {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("format sheet");
|
Sheet sheet = wb.createSheet("format sheet");
|
||||||
PrintSetup ps = sheet.getPrintSetup();
|
PrintSetup ps = sheet.getPrintSetup();
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class HeadersAndFooters {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("first-header - format sheet");
|
Sheet sheet = wb.createSheet("first-header - format sheet");
|
||||||
sheet.createRow(0).createCell(0).setCellValue(123);
|
sheet.createRow(0).createCell(0).setCellValue(123);
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class HyperlinkExample {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception{
|
public static void main(String[]args) throws Exception{
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
CreationHelper createHelper = wb.getCreationHelper();
|
CreationHelper createHelper = wb.getCreationHelper();
|
||||||
|
|
||||||
//cell style for hyperlinks
|
//cell style for hyperlinks
|
||||||
|
@ -138,7 +138,7 @@ public class LoanCalculator {
|
|||||||
/**
|
/**
|
||||||
* cell styles used for formatting calendar sheets
|
* cell styles used for formatting calendar sheets
|
||||||
*/
|
*/
|
||||||
public static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
|
private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
|
||||||
Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
|
Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
|
||||||
|
|
||||||
XSSFCellStyle style;
|
XSSFCellStyle style;
|
||||||
|
@ -32,7 +32,7 @@ import java.io.FileOutputStream;
|
|||||||
*/
|
*/
|
||||||
public class MergingCells {
|
public class MergingCells {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
Row row = sheet.createRow((short) 1);
|
Row row = sheet.createRow((short) 1);
|
||||||
|
@ -31,7 +31,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||||||
public class NewLinesInCells {
|
public class NewLinesInCells {
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet();
|
Sheet sheet = wb.createSheet();
|
||||||
|
|
||||||
Row row = sheet.createRow(2);
|
Row row = sheet.createRow(2);
|
||||||
|
@ -26,7 +26,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|||||||
public class SelectedSheet {
|
public class SelectedSheet {
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("row sheet");
|
Sheet sheet = wb.createSheet("row sheet");
|
||||||
|
|
||||||
Sheet sheet2 = wb.createSheet("another sheet");
|
Sheet sheet2 = wb.createSheet("another sheet");
|
||||||
|
@ -30,7 +30,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||||||
public class ShiftRows {
|
public class ShiftRows {
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("Sheet1");
|
Sheet sheet = wb.createSheet("Sheet1");
|
||||||
|
|
||||||
Row row1 = sheet.createRow(1);
|
Row row1 = sheet.createRow(1);
|
||||||
|
@ -149,7 +149,7 @@ public class TimesheetDemo {
|
|||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
|
private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
|
||||||
Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
|
Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
|
||||||
XSSFCellStyle style;
|
XSSFCellStyle style;
|
||||||
XSSFFont titleFont = wb.createFont();
|
XSSFFont titleFont = wb.createFont();
|
||||||
|
@ -27,7 +27,7 @@ import java.io.FileOutputStream;
|
|||||||
*/
|
*/
|
||||||
public class WorkingWithBorders {
|
public class WorkingWithBorders {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("borders");
|
Sheet sheet = wb.createSheet("borders");
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
// Create a row and put some cells in it. Rows are 0 based.
|
||||||
|
@ -27,7 +27,7 @@ import java.io.FileOutputStream;
|
|||||||
*/
|
*/
|
||||||
public class WorkingWithFonts {
|
public class WorkingWithFonts {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
Sheet sheet = wb.createSheet("Fonts");
|
Sheet sheet = wb.createSheet("Fonts");
|
||||||
|
|
||||||
Font font0 = wb.createFont();
|
Font font0 = wb.createFont();
|
||||||
|
@ -29,7 +29,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||||||
public class WorkingWithPageSetup {
|
public class WorkingWithPageSetup {
|
||||||
|
|
||||||
public static void main(String[]args) throws Exception {
|
public static void main(String[]args) throws Exception {
|
||||||
Workbook wb = new XSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the Workbook object.
|
* It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the Workbook object.
|
||||||
|
@ -32,7 +32,7 @@ public class WorkingWithPictures {
|
|||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
//create a new workbook
|
//create a new workbook
|
||||||
XSSFWorkbook wb = new XSSFWorkbook();
|
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
|
|
||||||
//add a picture in this workbook.
|
//add a picture in this workbook.
|
||||||
InputStream is = new FileInputStream("lilies.jpg");
|
InputStream is = new FileInputStream("lilies.jpg");
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
package org.apache.poi.xssf.usermodel.examples;
|
package org.apache.poi.xssf.usermodel.examples;
|
||||||
|
|
||||||
import org.apache.poi.xssf.usermodel.*;
|
import org.apache.poi.xssf.usermodel.*;
|
||||||
import org.apache.poi.ss.usermodel.*;
|
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
|
||||||
@ -28,29 +27,35 @@ public class WorkingWithRichText {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
XSSFWorkbook wb = new XSSFWorkbook();
|
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
|
|
||||||
XSSFSheet sheet = wb.createSheet();
|
XSSFSheet sheet = wb.createSheet();
|
||||||
XSSFRow row = sheet.createRow((short) 2);
|
XSSFRow row = sheet.createRow((short) 2);
|
||||||
|
|
||||||
XSSFCell cell = row.createCell(1);
|
XSSFCell cell = row.createCell(1);
|
||||||
XSSFRichTextString rt = new XSSFRichTextString("The quick");
|
XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");
|
||||||
|
|
||||||
XSSFFont font1 = wb.createFont();
|
XSSFFont font1 = wb.createFont();
|
||||||
font1.setBold(true);
|
font1.setBold(true);
|
||||||
rt.append(" brown fox", font1);
|
font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));
|
||||||
|
rt.applyFont(0, 10, font1);
|
||||||
|
|
||||||
XSSFFont font2 = wb.createFont();
|
XSSFFont font2 = wb.createFont();
|
||||||
font2.setItalic(true);
|
font2.setItalic(true);
|
||||||
font2.setColor(IndexedColors.RED.getIndex());
|
font2.setUnderline(XSSFFont.U_DOUBLE);
|
||||||
rt.applyFont((short) 0);
|
font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
|
||||||
|
rt.applyFont(10, 19, font2);
|
||||||
|
|
||||||
|
XSSFFont font3 = wb.createFont();
|
||||||
|
font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));
|
||||||
|
rt.append(" Jumped over the lazy dog", font3);
|
||||||
|
|
||||||
cell.setCellValue(rt);
|
cell.setCellValue(rt);
|
||||||
|
|
||||||
// Write the output to a file
|
// Write the output to a file
|
||||||
FileOutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx");
|
FileOutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx");
|
||||||
wb.write(fileOut);
|
wb.write(fileOut);
|
||||||
fileOut.close();
|
fileOut.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,9 @@ public class XSSFCreationHelper implements CreationHelper {
|
|||||||
* Creates a new XSSFRichTextString for you.
|
* Creates a new XSSFRichTextString for you.
|
||||||
*/
|
*/
|
||||||
public XSSFRichTextString createRichTextString(String text) {
|
public XSSFRichTextString createRichTextString(String text) {
|
||||||
return new XSSFRichTextString(text);
|
XSSFRichTextString rt =new XSSFRichTextString(text);
|
||||||
|
rt.setStylesTableReference(workbook.getStylesSource());
|
||||||
|
return rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFDataFormat createDataFormat() {
|
public XSSFDataFormat createDataFormat() {
|
||||||
|
@ -68,7 +68,6 @@ import java.util.ArrayList;
|
|||||||
public class XSSFRichTextString implements RichTextString {
|
public class XSSFRichTextString implements RichTextString {
|
||||||
private CTRst st;
|
private CTRst st;
|
||||||
private StylesTable styles;
|
private StylesTable styles;
|
||||||
private ArrayList<CTRPrElt> fontIdRuns;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a rich text string and initialize it with empty string
|
* Create a rich text string and initialize it with empty string
|
||||||
@ -106,7 +105,6 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
//when setStylesTableReference is called
|
//when setStylesTableReference is called
|
||||||
font = new XSSFFont();
|
font = new XSSFFont();
|
||||||
font.setFontName("#" + fontIndex);
|
font.setFontName("#" + fontIndex);
|
||||||
fontIdRuns = new ArrayList<CTRPrElt>();
|
|
||||||
} else {
|
} else {
|
||||||
font = styles.getFontAt(fontIndex);
|
font = styles.getFontAt(fontIndex);
|
||||||
}
|
}
|
||||||
@ -139,50 +137,50 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
XSSFFont xssfFont = (XSSFFont)font;
|
XSSFFont xssfFont = (XSSFFont)font;
|
||||||
ArrayList<CTRElt> runs = new ArrayList<CTRElt>();
|
ArrayList<CTRElt> runs = new ArrayList<CTRElt>();
|
||||||
|
|
||||||
|
CTRElt[] r = st.getRArray();
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
int i;
|
for (int i = 0; i < r.length; i++) {
|
||||||
for (i = 0; i < st.sizeOfRArray(); i++) {
|
int rStart = pos;
|
||||||
CTRElt r = st.getRArray(i);
|
String t = r[i].getT();
|
||||||
|
int rEnd = rStart + t.length();
|
||||||
|
|
||||||
int len = r.getT().length();
|
if(rEnd <= startIndex) {
|
||||||
int p1 = pos;
|
runs.add(r[i]);
|
||||||
int p2 = pos + len;
|
pos += r[i].getT().length();
|
||||||
if(startIndex > p2) {
|
}
|
||||||
runs.add(r);
|
else if (startIndex > rStart && startIndex < rEnd){
|
||||||
} else if (startIndex >= p1 && startIndex < p2){
|
CTRElt c = (CTRElt)r[i].copy();
|
||||||
String t = r.getT();
|
String txt = text.substring(rStart, startIndex);
|
||||||
r.setT(t.substring(0, startIndex-p1));
|
c.setT(txt);
|
||||||
runs.add(r);
|
runs.add(c);
|
||||||
|
pos += txt.length();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pos = p2;
|
|
||||||
}
|
}
|
||||||
CTRElt r = CTRElt.Factory.newInstance();
|
CTRElt rt = CTRElt.Factory.newInstance();
|
||||||
r.setT(text.substring(startIndex, endIndex));
|
String txt = text.substring(startIndex, endIndex);
|
||||||
CTRPrElt pr = r.addNewRPr();
|
rt.setT(txt);
|
||||||
|
CTRPrElt pr = rt.addNewRPr();
|
||||||
setRunAttributes(xssfFont.getCTFont(), pr);
|
setRunAttributes(xssfFont.getCTFont(), pr);
|
||||||
if(fontIdRuns != null) fontIdRuns.add(pr);
|
runs.add(rt);
|
||||||
runs.add(r);
|
pos += txt.length();
|
||||||
|
|
||||||
for (; i < st.sizeOfRArray(); i++) {
|
for (int i = 0; i < r.length; i++) {
|
||||||
r = st.getRArray(i);
|
int rStart = pos;
|
||||||
|
String t = r[i].getT();
|
||||||
|
int rEnd = Math.min(rStart + t.length(), text.length());
|
||||||
|
|
||||||
int len = r.getT().length();
|
if (endIndex < rEnd){
|
||||||
int p1 = pos;
|
CTRElt c = (CTRElt)r[i].copy();
|
||||||
int p2 = pos + len;
|
txt = text.substring(rStart, rEnd);
|
||||||
if(endIndex > p2) {
|
c.setT(txt);
|
||||||
;
|
runs.add(c);
|
||||||
} else if (endIndex >= p1 && endIndex < p2){
|
pos += txt.length();
|
||||||
String t = r.getT();
|
|
||||||
r.setT(t.substring(endIndex-p1, len));
|
|
||||||
runs.add(r);
|
|
||||||
} else {
|
|
||||||
runs.add(r);
|
|
||||||
}
|
}
|
||||||
pos = p2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
st.setRArray(runs.toArray(new CTRElt[runs.size()]));
|
st.setRArray(runs.toArray(new CTRElt[runs.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,9 +200,6 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr());
|
setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr());
|
||||||
st.setRArray(new CTRElt[]{r});
|
st.setRArray(new CTRElt[]{r});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fontIdRuns != null) fontIdRuns.add(st.getRArray(0).getRPr());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -217,7 +212,6 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
if(styles == null) {
|
if(styles == null) {
|
||||||
font = new XSSFFont();
|
font = new XSSFFont();
|
||||||
font.setFontName("#" + fontIndex);
|
font.setFontName("#" + fontIndex);
|
||||||
fontIdRuns = new ArrayList<CTRPrElt>();
|
|
||||||
} else {
|
} else {
|
||||||
font = styles.getFontAt(fontIndex);
|
font = styles.getFontAt(fontIndex);
|
||||||
}
|
}
|
||||||
@ -240,8 +234,6 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
lt.setT(text);
|
lt.setT(text);
|
||||||
CTRPrElt pr = lt.addNewRPr();
|
CTRPrElt pr = lt.addNewRPr();
|
||||||
if(font != null) setRunAttributes(font.getCTFont(), pr);
|
if(font != null) setRunAttributes(font.getCTFont(), pr);
|
||||||
|
|
||||||
if(fontIdRuns != null) fontIdRuns.add(pr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -419,9 +411,10 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
|
|
||||||
protected void setStylesTableReference(StylesTable tbl){
|
protected void setStylesTableReference(StylesTable tbl){
|
||||||
styles = tbl;
|
styles = tbl;
|
||||||
if(fontIdRuns != null){
|
if(st.sizeOfRArray() > 0) {
|
||||||
for (CTRPrElt pr : fontIdRuns) {
|
for (CTRElt r : st.getRArray()) {
|
||||||
if(pr.sizeOfRFontArray() > 0 ) {
|
CTRPrElt pr = r.getRPr();
|
||||||
|
if(pr != null){
|
||||||
String fontName = pr.getRFontArray(0).getVal();
|
String fontName = pr.getRFontArray(0).getVal();
|
||||||
if(fontName.startsWith("#")){
|
if(fontName.startsWith("#")){
|
||||||
int idx = Integer.parseInt(fontName.substring(1));
|
int idx = Integer.parseInt(fontName.substring(1));
|
||||||
|
@ -332,7 +332,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
|
|||||||
pane.setTopLeftCell(new CellReference(0, topRow).formatAsString());
|
pane.setTopLeftCell(new CellReference(0, topRow).formatAsString());
|
||||||
pane.setActivePane(STPane.TOP_RIGHT);
|
pane.setActivePane(STPane.TOP_RIGHT);
|
||||||
} else if (colSplit == 0) {
|
} else if (colSplit == 0) {
|
||||||
pane.setTopLeftCell(new CellReference(leftmostColumn, 64).formatAsString());
|
pane.setTopLeftCell(new CellReference(rowSplit, 0).formatAsString());
|
||||||
pane.setActivePane(STPane.BOTTOM_LEFT);
|
pane.setActivePane(STPane.BOTTOM_LEFT);
|
||||||
} else {
|
} else {
|
||||||
pane.setTopLeftCell(new CellReference(leftmostColumn, topRow).formatAsString());
|
pane.setTopLeftCell(new CellReference(leftmostColumn, topRow).formatAsString());
|
||||||
|
@ -28,11 +28,10 @@ public class TestXSSFName extends TestCase {
|
|||||||
// Create a new workbook
|
// Create a new workbook
|
||||||
XSSFWorkbook wb = new XSSFWorkbook();
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
|
||||||
|
|
||||||
// Create a worksheet 'sheet1' in the new workbook
|
|
||||||
XSSFName name1 = wb.createName();
|
XSSFName name1 = wb.createName();
|
||||||
name1.setNameName("testOne");
|
name1.setNameName("testOne");
|
||||||
|
|
||||||
|
//setting a duplicate name should throw IllegalArgumentException
|
||||||
XSSFName name2 = wb.createName();
|
XSSFName name2 = wb.createName();
|
||||||
try {
|
try {
|
||||||
name2.setNameName("testOne");
|
name2.setNameName("testOne");
|
||||||
|
@ -71,7 +71,7 @@ public class TestXSSFRichTextString extends TestCase {
|
|||||||
|
|
||||||
rt.applyFont(2, 5, font1);
|
rt.applyFont(2, 5, font1);
|
||||||
|
|
||||||
assertEquals(4, rt.numFormattingRuns());
|
assertEquals(5, rt.numFormattingRuns());
|
||||||
assertEquals(0, rt.getIndexOfFormattingRun(0));
|
assertEquals(0, rt.getIndexOfFormattingRun(0));
|
||||||
assertEquals(2, rt.getLengthOfFormattingRun(0));
|
assertEquals(2, rt.getLengthOfFormattingRun(0));
|
||||||
|
|
||||||
@ -79,10 +79,10 @@ public class TestXSSFRichTextString extends TestCase {
|
|||||||
assertEquals(3, rt.getLengthOfFormattingRun(1));
|
assertEquals(3, rt.getLengthOfFormattingRun(1));
|
||||||
|
|
||||||
assertEquals(5, rt.getIndexOfFormattingRun(2));
|
assertEquals(5, rt.getIndexOfFormattingRun(2));
|
||||||
assertEquals(2, rt.getLengthOfFormattingRun(2));
|
assertEquals(3, rt.getLengthOfFormattingRun(2));
|
||||||
|
|
||||||
assertEquals(7, rt.getIndexOfFormattingRun(3));
|
assertEquals(8, rt.getIndexOfFormattingRun(3));
|
||||||
assertEquals(2, rt.getLengthOfFormattingRun(3));
|
assertEquals(1, rt.getLengthOfFormattingRun(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testClearFormatting() throws Exception {
|
public void testClearFormatting() throws Exception {
|
||||||
|
Loading…
Reference in New Issue
Block a user