Question

I use the code that you can see below in order to strike through some cells in a jtable. Works great for me, and both in appearence and in print with JTable.print() method works excellent.

public void strikeThrough()
{
   int rows=jTable.getRowCount();
   destroiedDocs=new ArrayList<>();
   for (int i = 0; i < rows; i++)      
     {
       String test=String.valueOf(jTable.getValueAt(i,5));
       if (test.equals("K"))
         {
          for (int j = 1; j < 5; j++)
            {
            String tostrike= String.valueOf(jTable.getValueAt(i, j));
            String striked=("<html><strike>".concat(tostrike).concat("</strike>       </html>"));
            jTable.setValueAt(striked, i, j);
            destroiedDocs.add(i);
            }
         } 
     }
 }

The problem is that when I export the resaults to excel with the jxl class I got the value in these strike through cells with the appearence

<html><strike>some text</strike></html>

instead of some text in strikethrough. Any recomandations on how to solve this formating problem? I pass the cells in the excel cell by cell from the jtable with loops. Thank you!

Was it helpful?

Solution

The JAVA code:

String striked=("<html><strike>".concat(tostrike).concat("</strike>       </html>"));
jTable.setValueAt(striked, i, j);

Will work only for JAVA UI. After exporting data to excel it will show original text.
You need to perform following steps:
1. Before exporting remove <html><strike> and </strike></html>. Keep original text.
2. Use following code to strikethrough cell.

Run the following code using jxl-2.6.10.jar

import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelCreate {

  public static void main(String[] args) {
    try {

         WritableWorkbook workbook =
         Workbook.createWorkbook(new File("output.xls"));
         WritableSheet sheet = workbook.createSheet("Page1", 0);

         String Label[] = new String[4];
         Label[0] = "Emp ID";
         Label[1] = "Name";
         Label[2] = "Department";
         Label[3] = "Designation";

         WritableCellFormat cellFormat = new WritableCellFormat();
         WritableFont font = new WritableFont(WritableFont.ARIAL);
         font.setStruckout(true);
         cellFormat.setFont(font);

         for (int i = 0; i < Label.length; i++) {
             Label label = new Label(i, 0, Label[i]);
             sheet.addCell(label);
             WritableCell cell = sheet.getWritableCell(i, 0);
             cell.setCellFormat(cellFormat);
         }

         workbook.write();
         workbook.close();

    } catch (Exception e) {
        /* Write your logic to handle exception. 
         */
    }
  }
}

OTHER TIPS

I think before export each cell to the Excel you'll have to find and replace or remove the tags like

<html> 
    and
<strike>

with appropriate functions to add the required formatting..

Which API are you using to export it to Excel Format?? I think it'll be Specific the one that you're using to export to Excel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top