All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.databene.edifatto.gui.template.ExcelSheetTable Maven / Gradle / Ivy

/*
 * Copyright (C) 2013-2015 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * Licensed 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.databene.edifatto.gui.template;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.databene.formats.xls.XLSUtil;

/**
 * Displays the contents of an Excel sheet in a JTable.
 * Created: 15.08.2014 14:07:09
 * @since 2.0.0
 * @author Volker Bergmann
 */
public class ExcelSheetTable extends JTable {
	
	private static final long serialVersionUID = 1L;

	public ExcelSheetTable(Sheet sheet) {
		super(new ExcelSheetTableModel(sheet));
	}

	public static class ExcelSheetTableModel extends AbstractTableModel {
		
		private static final long serialVersionUID = 1L;
		
		private Sheet sheet;
		private int rowCount;
		private int columnCount;

		public ExcelSheetTableModel(Sheet sheet) {
			this.sheet = sheet;
			this.rowCount = sheet.getLastRowNum();
			this.columnCount = XLSUtil.getColumnCount(sheet);
		}

		@Override
		public int getColumnCount() {
			return columnCount;
		}
		
		@Override
		public String getColumnName(int column) {
			Row headerRow = sheet.getRow(0);
			if (headerRow == null)
				return null;
			Cell cell = headerRow.getCell(column);
			return XLSUtil.resolveCellValueAsString(cell);
		}

		@Override
		public int getRowCount() {
			return rowCount;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			Row row = sheet.getRow(rowIndex + 1);
			if (row == null)
				return null;
			Cell cell = row.getCell(columnIndex);
			return XLSUtil.resolveCellValueAsString(cell);
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy