Java, 如何从Excel中读取数据到文件中

package demoIO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadDataFromExcepFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Begin to read data from Excel file.");
		try 
		{
			Workbook book = Workbook.getWorkbook(new File("D:\\Program Files\\eclipse3.6.1\\WorkSpace\\practice\\TPID.xls"));
			File file = new File("D:\\Program Files\\eclipse3.6.1\\WorkSpace\\practice\\tpid.txt");	
			if (file.exists())
			{
				file.delete();
			}			
			
			file.createNewFile();

			if (!file.canWrite())
			{
				System.out.println("This file can not be written. File name = " + file.getName());
			}
			else 
			{
				StringBuilder strHcom = new StringBuilder();
				StringBuilder strEcom = new StringBuilder();
				OutputStream out = null;
				out = new FileOutputStream(file);
				Sheet sheet = book.getSheet(0);
				int row = sheet.getRows();				
				for (int i = 1; i < row; i++ )
				{
					Cell cellPOSName = sheet.getCell(1, i);//The first is column number, the second is row number.
					Cell cellKeyCell = sheet.getCell(0, i);	
					//String posName = "#" + cellPOSName.getContents() + "\r\n";
					//String content = "search/supportedHotelTypes." + cellKeyCell.getContents() + "=ESR,DA,EEM" + "\r\n";
					if (cellPOSName.getContents().contains("Hcom"))
					{
						strHcom.append("#" + cellPOSName.getContents() + "\r\n");
						strHcom.append("search/supportedHotelTypes." + cellKeyCell.getContents() + "=ESR,DA,EEM" + "\r\n");
					}
					else 
					{
						strEcom.append("#" + cellPOSName.getContents() + "\r\n");
						strEcom.append("search/supportedHotelTypes." + cellKeyCell.getContents() + "=ESR,DA,EEM" + "\r\n");
					}
					//out.write(posName.getBytes());
					//out.write(content.getBytes());
				}
				out.write(strHcom.toString().getBytes());
				out.write(strEcom.toString().getBytes());
				out.close();
			}
			System.out.println("Success");			
		}
		catch (FileNotFoundException e) 
		{
			// TODO: handle exception
			e.printStackTrace();
		}
		catch (BiffException e) 
		{
			// TODO Auto-generated catch block
			System.out.println("Exception: " + e.getMessage());
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			System.out.println("Exception: " + e.getMessage());
			e.printStackTrace();
		}		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值