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();
}
}
}
Java, 如何从Excel中读取数据到文件中
最新推荐文章于 2024-07-30 17:22:58 发布