I have now been trying for too long to remove an image from my XSSFSheet. I cannot find any information about this, but I would think that it has to be possible..
Is there any way to remove an image from my XSSFSheet? Even the official (?) apache poi website does not mention anything besides creating and reading images
I am now not far away from giving up and just copying everything except said image into a new sheet. Which is obviously not how this should be done. I don't think I would be able to sleep well for a week if I did that.
My last unsuccessful attempt was to use my code which moves images (I shared that code in this post) but instead of setting valid row numbers I would set null, but that's not possible since the parameter for setRow() is int (primitive type).
Then I tried setting a negative value for the anchor rows. While this technically removes the images, the excel file has to be repaired when it is opened the next time. The images are not being displayed.
I believe I would have to remove the relation from the XSSFDrawing too to completely remove the image (I think this after finding this custom implementation of XSSFDrawing) but I have no idea what is going on there...
I would be grateful for any kind of help here!
For XSSF this is not as simple as it sounds. There is HSSFPatriarch.removeShape but there is not something comparable in XSSFDrawing.
We must delete the picture itself inclusive the relations. And we must delete the shape's anchor from the drawing.
Example which goes trough all pictures in a sheet and deletes a picture if it's shape name is "Image 2":
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import java.io.*;
class ExcelDeleteImage {
public static void deleteCTAnchor(XSSFPicture xssfPicture) {
XSSFDrawing drawing = xssfPicture.getDrawing();
XmlCursor cursor = xssfPicture.getCTPicture().newCursor();
cursor.toParent();
if (cursor.getObject() instanceof org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor) {
for (int i = 0; i < drawing.getCTDrawing().getTwoCellAnchorList().size(); i++) {
if (cursor.getObject().equals(drawing.getCTDrawing().getTwoCellAnchorArray(i))) {
drawing.getCTDrawing().removeTwoCellAnchor(i);
System.out.println("TwoCellAnchor for picture " + xssfPicture + " was deleted.");
}
}
} else if (cursor.getObject() instanceof org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTOneCellAnchor) {
for (int i = 0; i < drawing.getCTDrawing().getOneCellAnchorList().size(); i++) {
if (cursor.getObject().equals(drawing.getCTDrawing().getOneCellAnchorArray(i))) {
drawing.getCTDrawing().removeOneCellAnchor(i);
System.out.println("OneCellAnchor for picture " + xssfPicture + " was deleted.");
}
}
} else if (cursor.getObject() instanceof org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTAbsoluteAnchor) {
for (int i = 0; i < drawing.getCTDrawing().getAbsoluteAnchorList().size(); i++) {
if (cursor.getObject().equals(drawing.getCTDrawing().getAbsoluteAnchorArray(i))) {
drawing.getCTDrawing().removeAbsoluteAnchor(i);
System.out.println("AbsoluteAnchor for picture " + xssfPicture + " was deleted.");
}
}
}
}
public static void deleteEmbeddedXSSFPicture(XSSFPicture xssfPicture) {
if (xssfPicture.getCTPicture().getBlipFill() != null) {
if (xssfPicture.getCTPicture().getBlipFill().getBlip() != null) {
if (xssfPicture.getCTPicture().getBlipFill().getBlip().getEmbed() != null) {
String rId = xssfPicture.getCTPicture().getBlipFill().getBlip().getEmbed();
XSSFDrawing drawing = xssfPicture.getDrawing();
drawing.getPackagePart().removeRelationship(rId);
drawing.getPackagePart().getPackage().deletePartRecursive(drawing.getRelationById(rId).getPackagePart().getPartName());
System.out.println("Picture " + xssfPicture + " was deleted.");
}
}
}
}
public static void deleteHSSFShape(HSSFShape shape) {
HSSFPatriarch drawing = shape.getPatriarch();
drawing.removeShape(shape);
System.out.println("Shape " + shape + " was deleted.");
}
public static void main(String[] args) throws Exception {
String filename = "ExcelWithImages.xlsx";
//String filename = "ExcelWithImages.xls";
InputStream inp = new FileInputStream(filename);
Workbook workbook = WorkbookFactory.create(inp);
Sheet sheet = workbook.getSheetAt(0);
Drawing drawing = sheet.getDrawingPatriarch();
XSSFPicture xssfPictureToDelete = null;
if (drawing instanceof XSSFDrawing) {
for (XSSFShape shape : ((XSSFDrawing)drawing).getShapes()) {
if (shape instanceof XSSFPicture) {
XSSFPicture xssfPicture = (XSSFPicture)shape;
String shapename = xssfPicture.getShapeName();
int row = xssfPicture.getClientAnchor().getRow1();
int col = xssfPicture.getClientAnchor().getCol1();
System.out.println("Picture " + "" + " with Shapename: " + shapename + " is located row: " + row + ", col: " + col);
if ("Image 2".equals(shapename)) xssfPictureToDelete = xssfPicture;
}
}
}
if (xssfPictureToDelete != null) deleteEmbeddedXSSFPicture(xssfPictureToDelete);
if (xssfPictureToDelete != null) deleteCTAnchor(xssfPictureToDelete);
HSSFPicture hssfPictureToDelete = null;
if (drawing instanceof HSSFPatriarch) {
for (HSSFShape shape : ((HSSFPatriarch)drawing).getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture hssfPicture = (HSSFPicture)shape;
int picIndex = hssfPicture.getPictureIndex();
String shapename = hssfPicture.getShapeName().trim();
int row = hssfPicture.getClientAnchor().getRow1();
int col = hssfPicture.getClientAnchor().getCol1();
System.out.println("Picture " + picIndex + " with Shapename: " + shapename + " is located row: " + row + ", col: " + col);
if ("Image 2".equals(shapename)) hssfPictureToDelete = hssfPicture;
}
}
}
if (hssfPictureToDelete != null) deleteHSSFShape(hssfPictureToDelete);
FileOutputStream out = new FileOutputStream(filename);
workbook.write(out);
out.close();
workbook.close();
}
}
This code is tested using apache poi 4.0.1 and it needs the full jar of all of the schemas ooxml-schemas-1.4.jar as mentioned in FAQ N10025
Related
I use HSSFListener to process record, but i don't know how to get cell comments.
I use EventExample from poi source code,
here is the code:
public class EventExample implements HSSFListener {
private SSTRecord sstrec;
#Override
public void processRecord(Record record)
{
//how to get comments here?
}
public static void main(String[] args) throws IOException
{
try (FileInputStream fin = new FileInputStream(args[0])) {
try (POIFSFileSystem poifs = new POIFSFileSystem(fin)) {
try (InputStream din = poifs.createDocumentInputStream("Workbook")) {
HSSFRequest req = new HSSFRequest();
req.addListenerForAllRecords(new EventExample());
HSSFEventFactory factory = new HSSFEventFactory();
factory.processEvents(req, din);
}
}
}
System.out.println("done.");
}
}
This is a really good question which seems not answered anywhere yet. Even Apache Tika handles cell comments as extraTextCells outside the worksheet and without relation to the cell.
If I have got i right, hen there are NoteRecords having cell relations. The NoteRecord.getShapeId points to an CommonObjectDataSubRecord of type CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT in an ObjRecord which is immediatelly followed by a TextObjectRecord. So to get the whole comment, we first need get the TextObjectRecords mapped to their shape Ids. Then we can get the NoteRecords and using their getShapeId methods we can get the related TextObjectRecords.
Example:
import org.apache.poi.hssf.eventusermodel.*;
import org.apache.poi.hssf.record.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Map;
import java.util.HashMap;
public class EventExample implements HSSFListener {
private SSTRecord sstrec;
private Map<Integer, TextObjectRecord> cellCommentRecs = new HashMap<Integer, TextObjectRecord>();
private int commentORecId = -1;
public void processRecord(Record record) {
switch (record.getSid()) {
case SSTRecord.sid: // shared strings table
sstrec = (SSTRecord) record;
break;
case BoundSheetRecord.sid:
BoundSheetRecord bsr = (BoundSheetRecord) record;
System.out.println("Sheet found named " + bsr.getSheetname());
break;
case NumberRecord.sid: // numeric cell
NumberRecord numrec = (NumberRecord) record;
System.out.println("Cell found with value " + numrec.getValue() + " at row " + numrec.getRow() + " and column " + numrec.getColumn());
break;
case LabelSSTRecord.sid: // string cell
LabelSSTRecord lrec = (LabelSSTRecord) record;
System.out.println("String cell found with value " + sstrec.getString(lrec.getSSTIndex()) + " at row " + lrec.getRow() + " and column " + lrec.getColumn());
break;
case ObjRecord.sid: // object record (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/dd34df60-8250-40a9-83a3-911476a31ea7)
ObjRecord orec = (ObjRecord) record;
for (SubRecord subrec : orec.getSubRecords()) {
if (subrec instanceof CommonObjectDataSubRecord) { // kind of shape (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/29161566-5018-4356-8d25-50e6674c66fa)
CommonObjectDataSubRecord codsrec = (CommonObjectDataSubRecord) subrec;
if (codsrec.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT) { // comment shape
//System.out.println(codsrec); //FtCmo
commentORecId = codsrec.getObjectId(); // we have a comment object record, so get it's Id
}
}
}
break;
case TextObjectRecord.sid: // text object (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/638c08e6-2942-4783-b71b-144ccf758fc7)
TextObjectRecord trec = (TextObjectRecord) record;
//System.out.println(trec); //TxO
if (commentORecId > -1) { // if we have a a comment object record Id already, so this is a comment text object
cellCommentRecs.put(commentORecId, trec); // map that Id to the text object record
commentORecId = -1;
}
break;
case NoteRecord.sid: // note record (https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/3a610bb3-9d35-435f-92ef-cdbc42974404)
NoteRecord nrec = (NoteRecord) record; // shapeId points to the comment shape which is immediately followed by the comment text object
System.out.println("Cell comment found at row " + nrec.getRow() + " and column " + nrec.getColumn() +
", author: " + nrec.getAuthor() + ", shape-id: " + nrec.getShapeId() +
", comment content: " + cellCommentRecs.get(nrec.getShapeId()).getStr().getString());
break;
default:
//System.out.println(record);
break;
}
}
public static void main(String[] args) throws Exception {
//FileInputStream fin = new FileInputStream(args[0]);
FileInputStream fin = new FileInputStream("./Excel.xls");
POIFSFileSystem poifs = new POIFSFileSystem(fin);
InputStream din = poifs.createDocumentInputStream("Workbook");
HSSFRequest req = new HSSFRequest();
req.addListenerForAllRecords(new EventExample());
HSSFEventFactory factory = new HSSFEventFactory();
factory.processEvents(req, din);
fin.close();
din.close();
System.out.println("done.");
}
}
Microsoft documentation: https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/7d9326d6-691a-4fa1-8dce-42082f38e943:
Note (section 2.4.179)
Obj (section 2.4.181) -> FtCmo: https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/29161566-5018-4356-8d25-50e6674c66fa
TxO (section 2.4.329)
I am new with Apache and I am checking that the image that I insert with the picture is resized in the word document. I am using the example that comes in the Apache documentation, just modified. The image is considerably enlarged from the original size and when the created .word document is opened, the picture is shown resized on document and I find no explanation, when I am forcing the size the picture should be.
Below is the code used:
public class SimpleImages {
public static void main(String\[\] args) throws IOException, InvalidFormatException {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
for (String imgFile : args) {
int format;
if (imgFile.endsWith(".emf")) {
format = XWPFDocument.PICTURE_TYPE_EMF;
} else if (imgFile.endsWith(".wmf")) {
format = XWPFDocument.PICTURE_TYPE_WMF;
} else if (imgFile.endsWith(".pict")) {
format = XWPFDocument.PICTURE_TYPE_PICT;
} else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
format = XWPFDocument.PICTURE_TYPE_JPEG;
} else if (imgFile.endsWith(".png")) {
format = XWPFDocument.PICTURE_TYPE_PNG;
} else if (imgFile.endsWith(".dib")) {
format = XWPFDocument.PICTURE_TYPE_DIB;
} else if (imgFile.endsWith(".gif")) {
format = XWPFDocument.PICTURE_TYPE_GIF;
} else if (imgFile.endsWith(".tiff")) {
format = XWPFDocument.PICTURE_TYPE_TIFF;
} else if (imgFile.endsWith(".eps")) {
format = XWPFDocument.PICTURE_TYPE_EPS;
} else if (imgFile.endsWith(".bmp")) {
format = XWPFDocument.PICTURE_TYPE_BMP;
} else if (imgFile.endsWith(".wpg")) {
format = XWPFDocument.PICTURE_TYPE_WPG;
} else {
System.err.println("Unsupported picture: " + imgFile +
". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
continue;
}
r.setText(imgFile);
r.addBreak();
try (FileInputStream is = new FileInputStream(imgFile)) {
BufferedImage bimg = ImageIO.read(new File(imgFile));
int anchoImagen = bimg.getWidth();
int altoImagen = bimg.getHeight();
System.out.println("anchoImagen: " + anchoImagen);
System.out.println("altoImagen: " + anchoImagen);
r.addPicture(is, format, imgFile, Units.toEMU(anchoImagen), Units.toEMU(altoImagen));
}
r.addBreak(BreakType.PAGE);
}
try (FileOutputStream out = new FileOutputStream("C:\\W_Ejm_Jasper\\example-poi-img\\src\\main\\java\\es\\eve\\example_poi_img\\images.docx")) {
doc.write(out);
System.out.println(" FIN " );
}
}
}
}
the image inside the word
the original image is (131 * 216 pixels):
the image is scaled in the word
I am trying to read images from xl_sheet using method wbook.getAllpicture(); but I found that these images are returning as whole I can not keep images separated according workshets.
In Excel the pictures data are stored on Workbook level. This is what Workbook.getAllPictures actually gets.
Each sheet has a Drawing layer which hovers over the sheet. In this Drawing are Shapes which are anchored to the sheet and may also be linked to the pictures data stored on Workbook level. If so, the shape does showing that picture.
So for getting the pictures dependent of the sheets, we must run over the appropriate Drawing layer, determining whether the shape we found is linked to a picture. If so, we get that picture.
Example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileInputStream;
class ExcelGetXSSFPicturesWithPosition {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelWithImages.xlsx"));
//Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelWithImages.xls"));
for (Sheet sheet : workbook) {
String sheetname = sheet.getSheetName();
Drawing drawing = sheet.getDrawingPatriarch();
if (drawing instanceof XSSFDrawing) {
for (XSSFShape shape : ((XSSFDrawing)drawing).getShapes()) {
if (shape instanceof XSSFPicture) {
XSSFPicture xssfPicture = (XSSFPicture)shape;
String shapename = xssfPicture.getShapeName();
int row = xssfPicture.getClientAnchor().getRow1();
int col = xssfPicture.getClientAnchor().getCol1();
System.out.println(
"Picture with Shapename: " + shapename +
" is located sheet: " + sheetname +
", row: " + row +
", col: " + col
);
}
}
} else if (drawing instanceof HSSFPatriarch) {
for (HSSFShape shape : ((HSSFPatriarch)drawing).getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture hssfPicture = (HSSFPicture)shape;
String shapename = hssfPicture.getShapeName();
int row = hssfPicture.getClientAnchor().getRow1();
int col = hssfPicture.getClientAnchor().getCol1();
System.out.println(
"Picture with Shapename: " + shapename +
" is located sheet: " + sheetname +
", row: " + row +
", col: " + col
);
}
}
}
}
workbook.close();
}
}
For getting the picture data out of the XSSFPicture we can use XSSFPicture.getPictureData. And for the HSSFPicture we can use HSSFPicture.getPictureData.
That both provides PictureData.getData which returns a byte[] containing the binary data of the picture.
I have some words in English that have been translated into Tamil. The task requires me to display them. An example line is given below. The first and second lines are in Tamil while the last is in Bengali.
> unpopular ஜனங்கலால் வெறுக்கப்பட்ட ¤µ£»ªÀ»õu
inactive ஜடமான ö\¯»ØÓ
doctor வைத்தியர் ©¸zxÁº
apart வேறாக uµ
If you notice above, the text in some lines does not render correctly because it is written in custom fonts. The custom font can be downloaded from here. My problem:
1. All Tamil fonts (custom and pre-loaded) have been installed. None of the text displays correctly. Why?
2. Is there a problem with the way that I am loading custom fonts?
3. In the above lines, the second column is pre-loaded font while the third column is written in custom fonts. The third column does not seem like it is Unicode, which is why application of any font also fails. What is going on?
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
/* This is the imageMaker class.
* It loads a plain white image, writes text taken from another file
* and creates a new image from it.
*
* Steps:
* 1. A plain white image is loaded.
* 2. Text is taken from a file.
* 3. Text is written to the image.
* 4. New image is created and saved.
*/
public class imgMaker_so {
/**
* #param args
*/
private static String tgtDir = "YOUR_tgt directory to store images goes here";
private static String csvFile = "csv file goes here";
private static int fontSize = 22; //default to a 22 pt font.
private static Font f;
private static String fontName = "WTAM001";
public static void main(String[] args) {
// TODO Auto-generated method stub
//Step 0. Read the image.
//readPlainImage(plainImg);
//Step 0.a: Check if the directory exists. If not, create it.
File tgtDir_file = new File(tgtDir);
if(!tgtDir_file.exists()) { //this directory does not exist.
tgtDir_file.mkdir();
}
Font nf = null;
try {
nf = Font.createFont(Font.TRUETYPE_FONT, new File("C:\\Windows\\Fonts\\" + fontName + ".ttf"));
} catch (FontFormatException | IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
if(nf != null) {
f = nf.deriveFont(Font.BOLD, fontSize);
}
if(f == null) {
System.out.println("Font is still null.");
}
//Step 1. Read csv file and get the string.
FileInputStream fis = null;
BufferedReader br = null;
try {
fis = new FileInputStream(new File(csvFile));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String temp = "\u0b85";
System.out.println(temp.length());
for(int i = 0; i < temp.length(); i++) {
System.out.print(temp.charAt(i));
}
//SAMPLE CODE ONLY. CHECK IF IT CAN PRINT A SINGLE CHARACTER IN FONT.
BufferedImage img = new BufferedImage(410, 200, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 410, 200);
System.out.println("String being printed = " + temp.codePointAt(0));
g.setColor(Color.BLACK);
g.setFont(f);
if(f.canDisplay('\u0b85')) {
System.out.println("Can display code = \u0b85");
} else {
System.out.println("Cannot display code = \u0b85");
}
g.drawString(temp, 10, 35);
//g.drawString(translation, 10, fontWidth); //a 22pt font is approx. 35 pixels long.
g.dispose();
try {
ImageIO.write(img, "jpeg", new File(tgtDir + "\\" + "a.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File written successfully to a");
//System.out.println("Cat,,बिल्ली,,,");
if(fis != null) {
try {
br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
System.out.print("Unsupported encoding");
}
String line = null;
if(br != null) {
try {
while((line = br.readLine()) != null) {
if(line != null) {
System.out.println("Line = " + line);
List<String> word_translation = new ArrayList<String>();
parseLine(line, word_translation); //function to parse the line.
//printImages(word_translation);
if(word_translation.size() > 0) {
printImages_temp(word_translation);
}
//now that images have been read, read the plain image afresh.
//readPlainImage(plainImg);
word_translation.clear();
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public static void printImages_temp(List<String> list) {
/* Function to print translations contained in list to images.
* Steps:
* 1. Take plain white image.
* 2. Write English word on top.
* 3. Take each translation and print one to each line.
*/
String dest = tgtDir + "\\" + list.get(0) + ".jpg"; //destination file image.
//compute height and width of image.
int img_height = list.size() * 35 + 20;
int img_width = 0;
int max_length = 0;
for(int i = 0; i < list.size(); i++) {
if(list.get(i).length() > max_length) {
max_length = list.get(i).length();
}
}
img_width = max_length * 20;
System.out.println("New dimensions of image = " + img_width + " " + img_height);
BufferedImage img = new BufferedImage(img_width, img_height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, img_width, img_height);
//image has to be written to another file. Do not write English word, which is why list starts iteration from 1.
for(int i = 1; i < list.size(); i++) {
System.out.println("String being printed = " + list.get(i).codePointAt(0));
g.setColor(Color.BLACK);
g.setFont(f);
g.drawString(list.get(i), 10, (i + 1) * 35);
}
//g.drawString(translation, 10, fontWidth); //a 22pt font is approx. 35 pixels long.
g.dispose();
try {
ImageIO.write(img, "jpeg", new File(dest));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File written successfully to " + dest);
}
public static void purge(String line) {
//This removes any inverted commas and tabs from the line apart from trimming it.
System.out.println("Line for purging = " + line);
int fromIndex = line.indexOf("\"");
//System.out.println("from index = " + fromIndex);
if(fromIndex != -1) {
line = line.substring((fromIndex + 1));
int toIndex = line.lastIndexOf("\"", line.length() - 1);
if(toIndex != -1) {
line = line.substring(0, (toIndex));
}
}
line.replaceAll("\t", " ");
line.trim();
System.out.println("Line after purging = " + line);
}
public static void parseLine(String line, List<String> result) {
/*
* This function parses the string and gets the different hindi meanings.
*/
//int index = line.indexOf(",");
//int prev_index = 0;
String[] arr = line.split(",");
List<String> l = new ArrayList<String>(Arrays.asList(arr));
for(int i = 0; i < l.size(); i++) {
if(l.get(i).isEmpty()) { //if the string at position i is empty.
l.remove(i);
}
}
for(int i = 0; i < l.size(); i++) { //inefficient copy but should be short.
String ith = l.get(i).trim();
if(!(ith.isEmpty())) { //add a string to result only if it is non-empty.
//in some entries, there are commas. they have been replaced with !?. find them and replace them.
if(ith.contains("!?")) {
//System.out.println(r + " contains !?");
String r = ith.replace("!?", ",");
result.add(r);
} else if(ith.contains("\n")) {
String r = ith.replace("\n", " ");
System.out.println("found new line in " + ith);
result.add(r);
} else {
result.add(ith);
}
}
}
for(int i = 0; i < result.size(); i++) {
System.out.println("Result[" + i + "] = " + result.get(i));
}
//System.out.println("Line being printed = " + line);
}
}
The above text was written by professional translators. So, is there something here that I am missing?
to test the concrete Font with methods in API Font.canDisplay
required to test in Unicode form (for mixing chars from a few languages (Tamil & Bengali))
please can you post and SSCCE, with used Font and can be based on
i want to convert my input Excel file into the Output XML file.
If anybody has any solution in java for how to take input Excel file and how to write to XML as output,please give any code or any URL or any other solution.
Thanks,
Mishal Shah
Look into the jexcel or Apache POI libraries for reading in the Excel file.
Creating an XML file is simple, either just write the XML out to a file directly, or append to an XML Document and then write that out using the standard Java libs or Xerces or similar.
I have done conversion of Excel(xlsx) to xml in Java recently. I assumed each row in excel as a single object here. Here are the steps I followed:-
Read Excel file using Apache POI
Created a xsd file and
generated corresponding classes
Read each row created, created corresponding objects and initilaized values using the generated getter/setter methods in the classes
Added the objects to an arraylist which holds only objects the same type
Using Jaxb Marshelled the arraylist object to an output file
Ready to provide code if required
Here's where you can start https://sites.google.com/site/arjunwebworld/Home/programming/jaxb-example
JExcel was easy for me to use. Put jxl.jar on the classpath and code something like:
File excelFile = new File(excelFilename);
// Create model for excel file
if (excelFile.exists()) {
try {
Workbook workbook = Workbook.getWorkbook(excelFile);
Sheet sheet = workbook.getSheets()[0];
TableModel model = new DefaultTableModel(sheet.getRows(), sheet.getColumns());
for (int row = 0; row < sheet.getRows(); row++) {
for (int column = 0; column < sheet.getColumns(); column++) {
String content = sheet.getCell(column, row).getContents();
model.setValueAt(content, row, column);
}
}
previewTable.setModel(model);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e);
}
} else {
JOptionPane.showMessageDialog(null, "File does not exist");
}
See http://jexcelapi.sourceforge.net/resources/faq/ to get started and link to download area.
File excelFile = new File(excelFilename);
// Create model for excel file
if (excelFile.exists()) {
try {
Workbook workbook = Workbook.getWorkbook(excelFile);
Sheet sheet = workbook.getSheets()[0];
TableModel model = new DefaultTableModel(sheet.getRows(), sheet.getColumns());
for (int row = 0; row < sheet.getRows(); row++) {
for (int column = 0; column < sheet.getColumns(); column++) {
String content = sheet.getCell(column, row).getContents();
model.setValueAt(content, row, column);
}
}
previewTable.setModel(model);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e);
}
} else {
JOptionPane.showMessageDialog(null, "File does not exist");
}
Download jxl and use this code
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.text.BadLocationException;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Font;
import jxl.read.biff.BiffException;
public class XlsToXml {
public String toXml(File excelFile) throws IOException, BiffException {
try {
String xmlLine = "";
String rowText = "";
String colText = "";
String isBold = "";
Font font = null;
String cellCol = "";
String cellAddress = "";
Cell cell = null;
Workbook workbook = Workbook.getWorkbook(excelFile);
xmlLine += "<workbook>" + "\n";
for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++) {
Sheet s = workbook.getSheet(sheet);
xmlLine += " <sheets>" + "\n";
Cell[] row = null;
for (int i = 0; i < s.getRows(); i++) {
row = s.getRow(i);
for (int j = 0; j < row.length; j++) {
if (row[j].getType() != CellType.EMPTY) {
cell = row[j];
cellCol=columnName(cell.getColumn());
cellCol=" colLetter=\""+cellCol+"\"";
cellAddress=" address=\""+cellAddress(cell.getRow()+1,cell.getColumn())+"\"";
isBold = cell.getCellFormat().getFont().getBoldWeight() == 700 ? "true" : "false";
isBold = (isBold == "false" ? "" : " isBold=\"true\"");
colText += " <col number=\"" + (j + 1) + "\"" + isBold +cellAddress+ ">";
colText += "<![CDATA[" + cell.getContents() + "]]>";
colText += "</col>" + "\n";
rowText += cell.getContents();
}
}
if (rowText != "") {
xmlLine += " <row number=\"" + (i + 1) + "\">" + "\n";
xmlLine += colText;
xmlLine += " </row>" + "\n";
}
colText = "";
rowText = "";
}
xmlLine += " </sheet>" + "\n";;
}
xmlLine += "</workbook>";
return xmlLine;
} catch (UnsupportedEncodingException e) {
System.err.println(e.toString());
}
return null;
}
private String cellAddress(Integer rowNumber, Integer colNumber){
//return "$"+columnName(colNumber)+"$"+rowNumber;
return columnName(colNumber)+rowNumber;
}
private String columnName(Integer colNumber) {
Base columns = new Base(colNumber,26);
columns.transform();
return columns.getResult();
}
class Base {
String[] colNames = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".split(",");
String equalTo;
int position;
int number;
int base;
int[] digits;
int[] auxiliar;
public Base(int n, int b) {
position = 0;
equalTo = "";
base = b;
number = n;
digits = new int[1];
}
public void transform() {
if (number < base) {
digits[position] = number;
size();
} else {
digits[position] = number % base;
size();
position++;
number = number / base;
transform();
}
}
public String getResult() {
for (int j = digits.length - 2; j >= 0; j--) {
equalTo += colNames[j>0?digits[j]-1:digits[j]];
}
return equalTo;
}
private void size() {
auxiliar = digits;
digits = new int[auxiliar.length + 1];
System.arraycopy(auxiliar, 0, digits, 0, auxiliar.length);
}
}
}