Main3.java Source code

Java tutorial

Introduction

Here is the source code for Main3.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.*;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author scottnumamoto
 */
public class Main3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {

        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("columA.xls"));
        HSSFSheet sheet = wb.getSheetAt(1);

        removeRow(sheet, 3);

        for (int i = 3; i < 4000; i++) {
            HSSFRow origRow = sheet.getRow(i);
            if (rowIsEmpty(origRow)) {
                System.out.println(i);
                //removeRow(sheet, i);
            }
        }

        FileOutputStream out = new FileOutputStream("okay4.xls");
        wb.write(out);
        out.close();
    }

    public static void removeRow(HSSFSheet sheet, int rowIndex) {
        int lastRowNum = sheet.getLastRowNum();
        if (rowIndex >= 0 && rowIndex < lastRowNum) {
            sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
        }
        if (rowIndex == lastRowNum) {
            HSSFRow removingRow = sheet.getRow(rowIndex);
            if (removingRow != null) {
                sheet.removeRow(removingRow);
            }
        }
    }

    private static void transferRow(HSSFRow rowA, HSSFRow rowB) {
        for (int c = 0; c < 9; c++) {
            HSSFCell cell1 = rowA.createCell(c);

            if (rowB.getCell(c) != null) {
                String replacement = rowB.getCell(c).getStringCellValue();
                System.out.println(replacement);
                cell1.setCellValue(replacement);
            }
        }
    }

    private static boolean rowIsEmpty(HSSFRow row) {
        if (row == null)
            return true;

        for (int c = 1; c < 9; c++) {
            HSSFCell cell = row.getCell(c);
            if (cell != null) {
                if (cell.getStringCellValue().length() > 0)
                    return false;
            }
        }
        return true;
    }

}