读取本地txt文件数据和excel文件数据
2016-09-08 22:12
836 查看
public class IniReader {
public static void readTxtFile(String filePath){
try {
//读取文本文件
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
test(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "D:\\ini\\config.txt";
readTxtFile(filePath);
Utils rw = new Utils();
String res = rw.readExcelFile("D:\\ini\\aaa.xls");
System.out.println("res="+res);
}
public static void test(String str){
String[] split = str.split(" ");
System.out.println("1="+split[0]);
System.out.println("2="+split[1]);
}
//读取本地excel文件数据
public class Utils {
public String readExcelFile(String filename) {
StringBuffer result = new StringBuffer();
String fileToBeRead = filename;
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
// 创建对工作表的引用。
HSSFSheet sheet = workbook.getSheetAt(0);
// HSSFSheet sheet = workbook.getSheet("Sheet1");
// 便利所有单元格,读取单元格
int row_num = sheet.getLastRowNum();
for (int i = 0; i < row_num+1; i++) {
HSSFRow r = sheet.getRow(i);
int cell_num = r.getLastCellNum();
for (int j = 0; j < cell_num; j++) {
//System.out.println(r.getCell((short)j).getCellType());
if(r.getCell((short)j).getCellType() == 1){
result.append(r.getCell((short) j).getStringCellValue());
}else{
result.append(r.getCell((short) j).getNumericCellValue());
}
result.append("\t");
}
result.append("\n");
}
} catch (FileNotFoundException e) {
System.out.println("文件没找到 : " + e);
} catch (IOException e) {
System.out.println("已运行IO异常: " + e);
}
return result.toString();
}
public void writeExcel(String path, String content) {
String outputFile = path;
String data = content;
try {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
HSSFSheet sheet = workbook.createSheet();
String[] rows = data.split("\n");
for (int i = 0; i < rows.length; i++) {
HSSFRow row = sheet.createRow((short) i);
String[] cells = rows[i].split("\t");
for (int j = 0; j < cells.length; j++) {
HSSFCell cell = row.createCell((short) j);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cells[j]);
}
}
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println(path + "文件生成完毕...");
} catch (FileNotFoundException e) {
System.out.println("文件没找到 : " + e);
} catch (IOException e) {
System.out.println("已运行IO异常 : " + e);
}
}
public String readWordFile(String filename) {
return "";
}
}
public static void readTxtFile(String filePath){
try {
//读取文本文件
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
test(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "D:\\ini\\config.txt";
readTxtFile(filePath);
Utils rw = new Utils();
String res = rw.readExcelFile("D:\\ini\\aaa.xls");
System.out.println("res="+res);
}
public static void test(String str){
String[] split = str.split(" ");
System.out.println("1="+split[0]);
System.out.println("2="+split[1]);
}
//读取本地excel文件数据
public class Utils {
public String readExcelFile(String filename) {
StringBuffer result = new StringBuffer();
String fileToBeRead = filename;
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
// 创建对工作表的引用。
HSSFSheet sheet = workbook.getSheetAt(0);
// HSSFSheet sheet = workbook.getSheet("Sheet1");
// 便利所有单元格,读取单元格
int row_num = sheet.getLastRowNum();
for (int i = 0; i < row_num+1; i++) {
HSSFRow r = sheet.getRow(i);
int cell_num = r.getLastCellNum();
for (int j = 0; j < cell_num; j++) {
//System.out.println(r.getCell((short)j).getCellType());
if(r.getCell((short)j).getCellType() == 1){
result.append(r.getCell((short) j).getStringCellValue());
}else{
result.append(r.getCell((short) j).getNumericCellValue());
}
result.append("\t");
}
result.append("\n");
}
} catch (FileNotFoundException e) {
System.out.println("文件没找到 : " + e);
} catch (IOException e) {
System.out.println("已运行IO异常: " + e);
}
return result.toString();
}
public void writeExcel(String path, String content) {
String outputFile = path;
String data = content;
try {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
HSSFSheet sheet = workbook.createSheet();
String[] rows = data.split("\n");
for (int i = 0; i < rows.length; i++) {
HSSFRow row = sheet.createRow((short) i);
String[] cells = rows[i].split("\t");
for (int j = 0; j < cells.length; j++) {
HSSFCell cell = row.createCell((short) j);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cells[j]);
}
}
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println(path + "文件生成完毕...");
} catch (FileNotFoundException e) {
System.out.println("文件没找到 : " + e);
} catch (IOException e) {
System.out.println("已运行IO异常 : " + e);
}
}
public String readWordFile(String filename) {
return "";
}
}
相关文章推荐
- 使用JDBC读取本地的Excel文件并批量更新数据
- Java 读取excel指定行列数据以及将数据保存到txt文件中
- 使用Java读取xlxs文件和写入txt文件,并将数据写入到本地文件
- java读取Excel数据,然后写入到txt文件,并批量保存到oracle数据库中
- R—读取数据(导入csv,txt,excel文件)
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 从本地目录读取指定.txt文件,并且对数据进行解析。
- R—读取数据(导入csv,txt,excel文件)
- C#读取Excel和txt文件数据并把数据分别一次性添加到Mysql数据库
- matlab读取txt,excel文件数据,图中图,局部放大
- 从txt文件逐行读取数据到excel文件(包含POI的简单使用和excel文件的数据分列,出现次数统计)
- Matlab读取Excel、txt文件数据
- SQL 数据的导入导出,对远程(MSsql,OracleAccess,)数据库的操作以及读取Excel,txt文件中的数据
- js读取本地txt文件中的json数据
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 读取Excel数据和写入txt文件以及读取配置文件工具类
- excel、csv、txt文件数据读取
- 网页端中将表格数据和JSON数据读取(导出)到本地的excel和csv文件中
- R—读取数据(导入csv,txt,excel文件)
- ASP.NET中上传并读取Excel文件数据,附后生成EXCEL及杀掉EXCEL进程。