NewMain.java Source code

Java tutorial

Introduction

Here is the source code for NewMain.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 NewMain {

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

        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("workbook.xls"));
        HSSFSheet sheet = wb.getSheetAt(0);

        int written = 2;

        //2997
        for (int r = 2; r < 2997; r += 6) {
            HSSFCell cell1 = sheet.getRow(r).getCell(1);
            HSSFCell cell2 = sheet.getRow(r + 1).getCell(1);
            HSSFCell cell3 = sheet.getRow(r + 2).getCell(1);

            Contact c = new Contact(cell1, cell2, cell3);
            System.out.println(c);

            HSSFRow row = sheet.getRow(written);
            if (row == null)
                row = sheet.createRow(written);

            HSSFCell cellComp = row.createCell(4);
            cellComp.setCellValue(c.getCompany());

            HSSFCell cellAdd = row.createCell(5);
            cellAdd.setCellValue(c.getAddress());

            HSSFCell cellCity = row.createCell(6);
            cellCity.setCellValue(c.getCity());

            HSSFCell cellState = row.createCell(7);
            cellState.setCellValue(c.getState());

            HSSFCell cellZip = row.createCell(8);
            cellZip.setCellValue(c.getZip());

            HSSFCell cellPhone = row.createCell(9);
            cellPhone.setCellValue(c.getPhone());

            HSSFCell cellSite = row.createCell(10);
            cellSite.setCellValue(c.getSite());

            written++;

        }

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

    }

}