读取Excel数据和写入txt文件以及读取配置文件工具类

来源:互联网 发布:moe域名注册 编辑:程序博客网 时间:2024/06/05 08:19

一、读取配置文件工具类ConfigHandler

package com.schmidt;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Properties;/** * @date 2015-07-16 * @author jyz * @version 1.0 *  */public class ConfigHandler{ private Properties properties; private InputStream in ; private InputStreamReader inr;public ConfigHandler(){properties = new Properties();in = getClass().getResourceAsStream("/config/config.properties");  try { inr = new InputStreamReader(in, "UTF-8"); properties.load(inr); if (inr != null) inr.close();} catch (Exception e) {e.printStackTrace();}} public String getProperty(String PropertyName){return properties.getProperty(PropertyName);}    public int getPropertyInt(String PropertyName){try { return  Integer.parseInt(properties.getProperty(PropertyName)); }catch (Exception e) {return -1;}}}

二、配置文件放在src下的config文件夹下config.properties,内容如下:

ExcelPath=D:/test1.xlssheetName=Sheet1sheetNumber=1Excel2File=D:/testResult.txtfromRow=0Comment=Comment Default=Default

三、读取Excel工具类ExcelHandler,需要额外添加jxl.jar包,其中有main函数,运行即可。

package com.schmidt;import java.io.File;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Writer;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;public class ExcelHandler {private static String sheetName="";//excel的sheet nameprivate static String ExcelPath="";//原始excel文件的路径private static String Excel2File="";//目标excel的路径private static int fromRow=1;//开始读取excel的行数private static int totalRow=0;private static int totalColumn=0;private static int sheetNumber=1;public static void main(String[] args) throws Exception {System.out.println("Excel Handle...");ConfigHandler config=new ConfigHandler();ExcelPath=config.getProperty("ExcelPath");Excel2File=config.getProperty("Excel2File");sheetName=config.getProperty("sheetName");fromRow=config.getPropertyInt("fromRow");sheetNumber=config.getPropertyInt("sheetNumber");System.out.println("Excel path:"+ExcelPath);System.out.println("Excel sheetName:"+sheetName);System.out.println("Excel Excel2File:"+Excel2File);readExcel(ExcelPath);}private static boolean readExcel(String path){System.out.println("readExcel....");try {//InputStream is=new FileInputStream();Workbook book=Workbook.getWorkbook(new File(path));if(book!=null){System.out.println("number:"+book.getNumberOfSheets());Sheet sh=book.getSheet(sheetName);if(sh==null)sh=book.getSheet(sheetNumber);totalRow=sh.getRows();totalColumn=sh.getColumns();StringBuffer sb=new StringBuffer();if(totalRow>=fromRow){//总行数大于开始读取excel的行数for(int i=fromRow;i<totalRow;i++){Cell[]cells=sh.getRow(i);//get the row i datasb.append(cells[0].getContents());//读取每行第一列的值}}if(sb!=null&&!sb.toString().equals("")){writeToFile(Excel2File,sb.toString());}}book.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();System.out.println(e.getMessage());return false;}finally{}return true;}/** * write the specific doc into specific path *  * @param path * @param doc */public static void writeToFile(String path, String message) {System.out.println("path:======"+path);//XMLWriter writer;//OutputFormat format = OutputFormat.createPrettyPrint();try {Writer wr=new OutputStreamWriter(new java.io.FileOutputStream(path),"UTF-8"); wr.write(message);wr.close();//writer = new XMLWriter(new FileWriter(path), format);//writer.write(doc);//writer.close();} catch (IOException e) {e.printStackTrace();}}}
四、工程文件目录图如下:



0 0
原创粉丝点击