Aspose cells to read table header row - java

How can i retrive excel table header row.
I able to retrive all rows except table header row.
Code
private static void printTableContent(final ListObject table) {
System.out.println(table.getShowHeaderRow());
Range range = table.getDataRange();
for (int row = 0; row < range.getRowCount(); row++) {
for (int column = 0; column < range.getColumnCount(); column++) {
System.out.print(range.get(row, column).getDisplayStringValue());
System.out.print("\t");
}
System.out.println();
}
}

ListObject.getDataRange() will only return the data rows.
To get the header row, use ListObject.getListColumns(). Add the following code in your method to print the list of header row.
for (int iColumn = 0 ; iColumn < table.getListColumns().getCount() ; iColumn++)
{
System.out.print(table.getListColumns().get(iColumn).getName());
}
System.out.println();
I work with Aspose as a Developer Evangelist.

Related

How to get the whole row of a JTable to save it into another table?

I am trying to validate that when the column step is equal to 2 the row is copied to another JTable.
But the jtable_step2 I have not initialized correctly,
that's why it returns the error:
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
How do I copy the same columns and rows that satisfy the condition?
Java code:
import javax.swing.JTable;
public class TestJTableCopy {
public static void main(String args[]){
String data[][]={ {"1","/LOCAL/USER/LOCAL", "20220421", "1"},
{"1","/LOACL/USER/LOCAL", "20220421", "2"},
{"1","/LOACL/USER/LOCAL", "20220422", "2"} };
String columns[] = {"LINE", "SOURCE", "DATE", "STEP"};
final JTable jtable = new JTable(data,columns);
JTable jtable_step2 = new JTable();
//jtable2.addColumn(columns);
int row = 0;
for (int i = 0; i < jtable.getRowCount(); i++) {
//STEP == 2
if (jtable.getValueAt(i, 3).equals("2")) {
for(int j = 0; j < jtable.getColumnCount(); j++) {
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
}
row++;
}
}
for (int i = 0; i < jtable_step2.getRowCount(); i++) {
for (int j = 0; j < jtable_step2.getColumnCount(); j++) {
System.out.println(i + " " + j + " " + jtable_step2.getValueAt(i, j));
}
}
}
}
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
You can't use the setValueAt(...) method because the row doesn't exist in the second table. You need to add a new row of data to the model of the table.
You do this by:
Creating a Vector, lets say "row"
Iterate through all the columns of the row you want to copy and add each item to the table
in the second table you use jtable_step2.addRow( row ) to add a new row of data to the model of the table.

Read excel and Assign cell Value by column to an array

Using apache.poi, I am trying to read an Excel sheet and using the below code, I am printing all the values inside Excel..
for (int i = 0; i < rowCount + 1; i++) {
Row row = searcsheet.getRow(i);
// Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
// Print Excel data in console
String location = (row.getCell(j).getStringCellValue()+ "");
System.out.println(location);
}
When I print the location System.out.println(location);, it prints my all Excel sheet data. I haven't any control over there. I am trying divide the value by the column.
Suppose I have 3 cells and I want to get a value like firstname[],lastname[],age[] so that I can do any operation by the indexing. I am not getting any solution.
Here is my full code
https://codeshare.io/anNwy3
Create a 2D String array. When iterating store the contents into the array
String[][] excelData = new String[numColumns][searcsheet.getLastRowNum()];
//numColumns is the number of columns in the excel sheet.
for (int i = 0; i < rowCount + 1; i++) {
Row row = searcsheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
String data = (row.getCell(j).getStringCellValue()+ "");
excelData[j][i] = data;
}
}
This transpose data when storing it into the array (note: excelData[j][i] and not excelData[i][j]).
With this, you can get all the contents of the first column as excelData[0].

Apache POI XWPFTableRow remove column Word

I am trying to create a function in Java which removes a column from a Word table with header name "Serious". Please find this function below. When I run the code nothing is happening. Has anyone had any issues using the removeCell function?
public static void remCells(XWPFTable table) {
for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {
XWPFTableRow row = table.getRow(rowIndex);
for (int colIndex = 0; colIndex < row.getTableCells().size()-1; colIndex++) {
XWPFTableCell cell = row.getCell(colIndex);
if(table.getRow(5).getCell(colIndex).getText().equals("Serious")) {
row.removeCell(colIndex);
}
}
}
}
Found solution here :
https://www.codota.com/code/java/methods/org.apache.poi.xwpf.usermodel.XWPFTableRow/removeCell
We have to do
row.getCtRow().removeTc(colIndex);
before
row.removeCell(colIndex);

Java Getting JTable Value (Per row)

I would like to get the value from the Jtable, and I tried it using the getvalueat however whenever I try to get the value from the JTable it only get the value from the first column of the selected row, I need to get all the value from the Jtable which I selected. Can you please help me with this one
here is my code:
class GetTableValue implements ActionListener{
public void actionPerformed(ActionEvent e){
AbstractButton button = (AbstractButton)e.getSource();
if(e.getActionCommand().equals(button.getActionCommand)){
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
Object data = (Object)table.getValueAt(row, col);
JOptionPane.showMessageDialog(null, data);
}
}
}
This is my action event where the value of the selected table is shown in the JOptionPane unfortunately it only display one value(which is the one you already selected) not the whole row.
This code is for my Jbutton for call the action event(I already excluded my code from the JTable since it fetch the Jtable value from my database)
ActionListener tableAction = new GetTableValue();
buttonEdit = new JButton("EDIT");
buttonEdit.addActionListener(tableAction);
the code is plain and simple, I also search Mr. G(google) about a good tutorial on fetching row, unfortunately there isn't a good tutorial for fetching Jtable value(per row).
If you want all the values from selected row then try this code.
int row = jTable1.getSelectedRow();
int column = jTable1.getColumnCount();
for(int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(row, i));
}
You get the all values for selected row, no matter how much columns are there in jtable
If you want all the values from jtable then try:
int row = jTable1.getRowCount();
int column = jTable1.getColumnCount();
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(j, i));
}
}
Yes you can use Object[] to store the values. For example:
Object[] val = new Object[column];
for (int k = 0; k < val.length - 1; k++) {
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
val[k] = jTable1.getValueAt(j, i);
System.out.println(val[k]);
}
}
}
getValueAt will return you the value of the cell (at row/col). Unless you're table model supports it, there is no convenient way (beyond what you are doing) to get the whole row in a single request.
Also, remember, if the table is sorted or filtered, the model indices will not match the view, you need to convert them first, using convertRowIndexToModel and convertColumnIndexToModel
UPDATE
The only way around it is if the table model you're using has a getRow (or equivalent) method. Without know how you are storing the data in the table model it's next to near impossible to give an accurate answer, but a general idea would be...
public class MyAwesomeTableModel extends AbstractTableModel {
// All the usual stuff...
public MyRowData getRowAt(int index) { ... }
}
Now, MyRowData is what ever implementation of the table data you've created. It could be (preferably) a single Object or in the case of the DefaultTableModel an array of objects.
class GetTableValue implements ActionListener{
public void actionPerformed(ActionEvent e){
AbstractButton button = (AbstractButton)e.getSource();
if(e.getActionCommand().equals(button.getActionCommand)){
int row = table.convertRowIndexToModel(table.getSelectedRow());
MyAwesomeTableModel model = (MyAwesomeTableModel)table.getModel();
MyRowData data = model.getRowAt(row);
JOptionPane.showMessageDialog(null, data);
}
}
}
This is all dependent on how you've implemented your TableModel and how you've implemented your row data, but that's the general jist
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int selectedRow;
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener()
{
#Override
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
int numCols = jTable1.getColumnCount();
model = (DefaultTableModel) jTable1.getModel();
System.out.print(" \n row " + selectedRow + ":");
for (int j = 0; j < numCols; j++)
{
System.out.print(" " + model.getValueAt(selectedRow, j));
}
}
});
}
Using this you can get value of whole row where u click on particular row.
you can try the below code to get selected row value:
int selectedRow = jTableName.getSelectedRow();
selectedRow = jTableName.convertRowIndexToModel(selectedRow);
String val1 = (String)jTableName.getModel().getValueAt(selectedRow, 0);
String val2 = (String)jTableName.getModel().getValueAt(selectedRow, 1);

searchings rows from table

i want to select particular rows from the JTable which contains a particular string.. please help me for this..
Something like this will do the trick:
void selectMatchingRows(JTable table, String regex)
{
for (int row = 0; row < table.getModel().getRowCount(); row++)
{
for (int col = 0; col < table.getModel().getColumnCount(); col++)
{
if (table.getModel().getValueAt(row, col).toString().matches(regex))
{
table.getSelectionModel().setSelectionInterval(row, row);
}
}
}
}
Making sure the ListSelectionModel.selectionMode is MULTIPLE_INTERVAL_SELECTION.
Call JTable.getModel then just loop through using TableModel.getValueAt

Categories