Nacitat z Excelu zapisat do suboru .xml pomocou eclipse

Programovacie jazyky, rady, poradňa...
cros
Novice
Novice
Príspevky: 1
Registrovaný: 25 máj 2011, 17:04

Nacitat z Excelu zapisat do suboru .xml pomocou eclipse

Príspevok od používateľa cros »

Ahojte,
potreboval by som veeeelku radu...som java neandrtalec a v praci som dostal zadanie v jave.
Potrebujem nacitat udaje z excelu a zapisat ich (este presne neviem aku formu ten zapis ma mat) do suboru .xml

spravil som nieco taketo

Kód: Vybrať všetko

package com.ms.util;

//~--- non-JDK imports --------------------------------------------------------
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.Iterator;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * This java program is used to read the data from a Excel file and display them
 * on the console output.
 */
public class POIExcelReader {

	/** Creates a new instance of POIExcelReader */
	public POIExcelReader() {
	}

	/**
	 * This method is used to display the Excel content to command line.
	 * 
	 * @param xlsPath
	 */
	@SuppressWarnings("unchecked")
	public void displayFromExcel(String xlsPath) {
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(xlsPath);
		} catch (FileNotFoundException e) {
			System.out.println("File not found in the specified path.");
			e.printStackTrace();
		}

		try {

			XSSFWorkbook workBook = new XSSFWorkbook(xlsPath);
			XSSFSheet sheet = workBook.getSheetAt(0);
			Iterator<Row> rows = sheet.rowIterator();

			while (rows.hasNext()) {
				XSSFRow row = (XSSFRow) rows.next();

				// display row number in the console.
				// System.out.println ("Row No.: " + row.getRowNum ());

				// once get a row its time to iterate through cells.
				// Iterator<XSSFCell> cells = row.cellIterator ();
				Iterator cells = row.cellIterator();

				while (cells.hasNext()) {
					XSSFCell cell = (XSSFCell) cells.next();

					// System.out.println ("Cell No.: " + cell.getCellNum ());

					/*
					 * Now we will get the cell type and display the values
					 * accordingly.
					 */
					switch (cell.getCellType()) {
					case XSSFCell.CELL_TYPE_NUMERIC: {

						// cell type numeric.
						// System.out.println ("Numeric value: " +
						// cell.getNumericCellValue ());
						System.out.println("" + cell.getNumericCellValue());

						break;
					}

					case XSSFCell.CELL_TYPE_STRING: {

						// cell type string.
						XSSFRichTextString richTextString = cell
								.getRichStringCellValue();

						// System.out.println ("String value: " +
						// richTextString.getString ());
						System.out.println("" + richTextString.getString());

						break;
					}

					default: {

						// types other than String and Numeric.
						System.out.println("Type not supported.");

						break;
					}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * The main executable method to test displayFromExcel method.
	 * 
	 * @param args
	 */
    public static void createFile(String name) {
    	   
        File file;
        try {
            file = new File(name);
            file.createNewFile();
        } catch(IOException vyjimka) {
            System.err.print(vyjimka);
        }
    }
	
	public static void main(String[] args)throws IOException {
		System.out.println("Specify fillepath.");
		String path = "";
		Scanner input = new Scanner(System.in);
		path = input.next();
		
		if (path.endsWith("xls")) {
			System.out.println("File not suported");				
		}
		else
		{
		
		System.out.println("______________________");
		POIExcelReader poiExample = new POIExcelReader();
		String xlsPath = path;
		poiExample.displayFromExcel(xlsPath);
		}
		
		String file = "novySoubor.xml";
        createFile(file);
        PrintWriter vystup = new PrintWriter(new FileWriter(file));
        vystup.println("Text, ktery se zapise do naseho souboru " + file +".");
        
        vystup.close();
	}
}
ja by som rad vedel ako tie ziskane udaje zapisem...a potreboval by som este aby sa ten .xml subor volal ako excel

to je zatial vsetko...snad sa najde niekto s dobrou radou...dik
Jasty
Light Star
Light Star
Používateľov profilový obrázok
Príspevky: 240
Registrovaný: 13 mar 2008, 19:22

Re: Nacitat z Excelu zapisat do suboru .xml pomocou eclipse

Príspevok od používateľa Jasty »

No ist cez POI je dobry plan, lebo to su velmi pekne kniznice. A ty vlastne ako zaciatocnik v jave mozes to xml vytvorit klasicky manualne kladanim riadkov retazcov do suboru (ale stracas tym komplexnost DOMu, pripadne vyhody schem - ale je to najviac primitivne). Ale k tomuto kroku mozes pristupit len vtedy, ked budes vediet presny format vystupu.
Napísať odpoveď