Доработать код - Java
Формулировка задачи:
Помогите пожалуйста доработать код. Есть код экспорта jtable в excel. Экспорт проходит хорошо, когда у таблицы одно строчный заголовок, а когда заголовок многострочный то заголовок экспортируется не полностью. на скриншотах показаны заголовки таблиц. вот код
помогите пожалуйста.
Листинг программы
- package javaapplication120;
- import java.io.*;
- import javax.swing.table.TableModel;
- import javax.swing.*;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.hssf.util.CellRangeAddress;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- 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.util.RegionUtil;
- public class ExcelExporter {
- ExcelExporter() {
- }
- public void exportTable(JTable jTable1, File file) throws IOException {
- FileOutputStream out = null;
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("new sheet");
- int colCount = jTable1.getColumnCount();
- int colRow = jTable1.getRowCount();
- Row row = null;
- Cell cell = null;
- TableModel model = jTable1.getModel();
- out = new FileOutputStream(file);
- //экспорт заголовка таблицы
- for (int j = 0; j <= jTable1.getRowCount(); j++) {
- row = sheet.createRow(j);
- for (int i = 0; i < model.getColumnCount(); i++) {
- cell = row.createCell(i);
- cell.setCellValue(model.getColumnName(i).toString());
- }
- }
- //экспорт данных из таблицы в ексель
- for (int i = 0; i < jTable1.getRowCount(); i++) {
- row = sheet.createRow(i + 1);
- int f = jTable1.getColumnCount();
- for (int j = 0; j < jTable1.getColumnCount(); j++) {
- cell = row.createCell(j);
- cell.setCellValue(jTable1.getValueAt(i, j).toString());
- }
- }
- //прорисовка границ в ексель
- for (int y = 0; y <= colRow; y++) {
- for (int x = 0; x < colCount; x++) {
- CellRangeAddress range = new CellRangeAddress(y, y, x, x);
- RegionUtil.setBorderTop(CellStyle.BORDER_THIN, range, sheet, wb);
- RegionUtil.setBorderRight(CellStyle.BORDER_THIN, range, sheet, wb);
- RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range, sheet, wb);
- RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, range, sheet, wb);
- }
- }
- for (int i = 0; i < colCount - 1; i++) {
- setBorder(colRow, colCount);
- sheet.autoSizeColumn(i);
- }
- wb.write(out);
- out.close();
- }
- private void setBorder(int i, int j) {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("new sheet");
- for (int y = 0; y <= i; y++) {
- for (int x = 0; x < j; x++) {
- CellRangeAddress range = new CellRangeAddress(y, y, x, x);
- RegionUtil.setBorderTop(CellStyle.BORDER_THIN, range, sheet,
- wb);
- RegionUtil.setBorderRight(CellStyle.BORDER_THIN, range, sheet,
- wb);
- RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range, sheet,
- wb);
- RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, range, sheet,
- wb);
- }
- }
- }
- }
Решение задачи: «Доработать код»
textual
Листинг программы
- package javaapplication120;
- import java.io.*;
- import javax.swing.table.TableModel;
- import javax.swing.*;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.hssf.util.CellRangeAddress;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- 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.util.RegionUtil;
- public class ExcelExporter {
- ExcelExporter() {
- }
- public void exportTable(JTable jTable1, File file) throws IOException {
- FileOutputStream out = null;
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("new sheet");
- int colCount = jTable1.getColumnCount();
- int colRow = jTable1.getRowCount();
- Row row = null;
- Cell cell = null;
- TableModel model = jTable1.getModel();
- out = new FileOutputStream(file);
- String a[] = {"Наименование", "ед.изм"};
- //экспорт заголовка таблицы
- sheet.shiftRows(0, jTable1.getColumnCount(), 1);
- sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
- sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
- for (int j = 0; j <= jTable1.getRowCount(); j++) {
- row = sheet.createRow(j);
- for (int i = 0; i < model.getColumnCount(); i++) {
- cell = row.createCell(i);
- cell.setCellValue(model.getColumnName(i).toString());
- }
- }
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 3));
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 5));
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 6, 7));
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 8, 9));
- //экспорт данных из таблицы в ексель
- for (int i = 0; i < jTable1.getRowCount(); i++) {
- row = sheet.createRow(i + 2);
- int f = jTable1.getColumnCount();
- for (int j = 0; j < jTable1.getColumnCount(); j++) {
- cell = row.createCell(j);
- cell.setCellValue(jTable1.getValueAt(i, j).toString());
- }
- }
- for (int k = 0; k < 1; k++) {
- row = sheet.createRow(k);
- for (int j = 0; j < 12; j++) {
- if (j < 2) {
- cell = row.createCell(j);
- cell.setCellValue(a[j].toString());
- } else {
- cell = row.createCell(j);
- switch (j) {
- case 2:
- cell.setCellValue("Остаток");
- break;
- case 4:
- cell.setCellValue("Приход(ДТ)");
- break;
- case 6:
- cell.setCellValue("Расход(КТ)");
- break;
- case 8:
- cell.setCellValue("Остаток");
- break;
- default:
- break;
- }
- }
- }
- }
- //прорисовка границ в ексель
- for (int y = 0; y <= colRow + 1; y++) {
- for (int x = 0; x < colCount; x++) {
- CellRangeAddress range = new CellRangeAddress(y, y, x, x);
- RegionUtil.setBorderTop(CellStyle.BORDER_THIN, range, sheet, wb);
- RegionUtil.setBorderRight(CellStyle.BORDER_THIN, range, sheet, wb);
- RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range, sheet, wb);
- RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, range, sheet, wb);
- }
- }
- for (int i = 0; i < colCount - 1; i++) {
- setBorder(colRow, colCount);
- sheet.autoSizeColumn(i);
- }
- sheet.setColumnWidth(0, 4000);
- sheet.setVerticallyCenter(Boolean.TRUE);
- wb.write(out);
- out.close();
- }
- private void setBorder(int i, int j) {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("new sheet");
- for (int y = 0; y <= i; y++) {
- for (int x = 0; x < j; x++) {
- CellRangeAddress range = new CellRangeAddress(y, y, x, x);
- RegionUtil.setBorderTop(CellStyle.BORDER_THIN, range, sheet,
- wb);
- RegionUtil.setBorderRight(CellStyle.BORDER_THIN, range, sheet,
- wb);
- RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range, sheet,
- wb);
- RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, range, sheet,
- wb);
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д