读取.xlsx文件内容并保存为.xlsx类型文件

工作中遇到了将1.xlsx,2.xlsx,3.xlsx,4.xlsx,5.xlsx几个文件的名称为newstoreinfo的sheet内容合并到info.xlsx文件里名称为storeinfo的sheet中,留作备用

package com.cn.my.xlsx;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


import com.cn.my.xlsx.NewStore;

//import lombok.experimental.var;

/**
 * 读取.xlsx文件内容并保存为.xlsx类型文件
 * @author wangc
 *
 */

public class ExportExcelTest {
	@SuppressWarnings("rawtypes")
	public static List<NewStore> getEmployeesList() throws Exception{
		List<NewStore> employeesList=new ArrayList<NewStore>();
		String files="D:\\newstore\\";//文件目录
		File file=new File(files);
		File [] dir=file.listFiles();//获取文件列表
		System.out.println(dir.length);
		for(int j=0;j<dir.length;j++) {
			String name=dir[j].getName();//获取该目录下所有文件的名称
			//判断文件后缀
			if(name.endsWith("xlsx")) {
				String pathname=files+name;//文件的绝对路径下面的getWorkbook需要
				System.out.println(pathname);
				InputStream iStream=new FileInputStream(pathname);

				//XSSFWorkbook book=new XSSFWorkbook(iStream);
				Workbook book=WorkbookFactory.create(iStream);
				
				
				//获取文件名为newstoreinfo的sheet
				//XSSFSheet sheet=book.getSheet("newstoreinfo");
				Sheet sheet=book.getSheet("newstoreinfo");
				//获取行数
				int rows=sheet.getLastRowNum();
				System.out.println("该excel表的数据有:"+rows+"行");
				//从第二行获取数据因为第一行是标题
				for(int i=1;i<=rows;i++) {
					NewStore info=new NewStore();
					info.setStoreName(sheet.getRow(i).getCell(0).getStringCellValue());
					System.out.println("StoreName:"+sheet.getRow(i).getCell(0).getStringCellValue());
					
					info.setStoreCode(sheet.getRow(i).getCell(1).getStringCellValue());
					System.out.println("StoreCode:"+sheet.getRow(i).getCell(1).getStringCellValue());
					
					info.setExternalCode(sheet.getRow(i).getCell(2).getStringCellValue());
					System.out.println("ExternalCode:"+sheet.getRow(i).getCell(2).getStringCellValue());
					
					info.setStores(sheet.getRow(i).getCell(3).getStringCellValue());
					System.out.println("Stores:"+sheet.getRow(i).getCell(3).getStringCellValue());
					
					info.setInventory(sheet.getRow(i).getCell(4).getStringCellValue());
					System.out.println("Inventory:"+sheet.getRow(i).getCell(4).getStringCellValue());
					
					info.setProvince(sheet.getRow(i).getCell(5).getStringCellValue());
					System.out.println("Province:"+sheet.getRow(i).getCell(5).getStringCellValue());
					
					info.setCity(sheet.getRow(i).getCell(6).getStringCellValue());
					System.out.println("City:"+sheet.getRow(i).getCell(6).getStringCellValue());
					
					info.setDirect(sheet.getRow(i).getCell(7).getStringCellValue());
					System.out.println("Direct:"+sheet.getRow(i).getCell(7).getStringCellValue());
					
					info.setAddress(sheet.getRow(i).getCell(8).getStringCellValue());
					System.out.println("Address:"+sheet.getRow(i).getCell(8).getStringCellValue());
					
					info.setContactPerson(sheet.getRow(i).getCell(9).getStringCellValue());
					System.out.println("ContactPerson:"+sheet.getRow(i).getCell(9).getStringCellValue());
					
					sheet.getRow(i).getCell(10).setCellType(HSSFCell.CELL_TYPE_STRING);
					info.setContactPhone(sheet.getRow(i).getCell(10).getStringCellValue());
					System.out.println("ContactPhone:"+sheet.getRow(i).getCell(10).getStringCellValue());
					
					sheet.getRow(i).getCell(11).setCellType(HSSFCell.CELL_TYPE_STRING);
					info.setPostalCode(sheet.getRow(i).getCell(11).getStringCellValue());
					System.out.println("PostalCode:"+sheet.getRow(i).getCell(11).getStringCellValue());
					
					sheet.getRow(i).getCell(12).setCellType(HSSFCell.CELL_TYPE_STRING);
					info.setLongitude(sheet.getRow(i).getCell(12).getStringCellValue());
					System.out.println("Longitude:"+sheet.getRow(i).getCell(12).getStringCellValue());
					
					sheet.getRow(i).getCell(13).setCellType(HSSFCell.CELL_TYPE_STRING);
					info.setLatitude(sheet.getRow(i).getCell(13).getStringCellValue());
					System.out.println("Latitude:"+sheet.getRow(i).getCell(13).getStringCellValue());

					
					employeesList.add(info);
					book.close();
				}
			}else {
				
				System.out.println(name);
			}
			
		}
		return employeesList;
		
	}
	

//	private static byte[] getBytes(InputStream iStream) {
//		// TODO Auto-generated method stub
//		return null;
//	}


	public static void main(String[] args) throws Exception{
		HSSFWorkbook workbook=new HSSFWorkbook();
		HSSFSheet sheet=workbook.createSheet("storeinfo");
		HSSFRow header=sheet.createRow(0);
		
		
		HSSFCell cell=header.createCell(0);
		cell.setCellValue("Name");
		
		cell=header.createCell(1);
		cell.setCellValue("Code");
		
		
		cell=header.createCell(2);
		cell.setCellValue("External");
		
		cell=header.createCell(3);
		cell.setCellValue("Distribution");
		
		cell=header.createCell(4);
		cell.setCellValue("Inventory");
		
		cell=header.createCell(5);
		cell.setCellValue("Province");
		
		cell=header.createCell(6);
		cell.setCellValue("City");
		
		cell=header.createCell(7);
		cell.setCellValue("Direct");
		
		cell=header.createCell(8);
		cell.setCellValue("Address");
		
		cell=header.createCell(9);
		cell.setCellValue("Person");
		
		cell=header.createCell(10);
		cell.setCellValue("Phone");
		
		cell=header.createCell(11);
		cell.setCellValue("PostalCode");
		
		cell=header.createCell(12);
		cell.setCellValue("Longitude");
		
		cell=header.createCell(13);
		cell.setCellValue("Latitude");
		
		
		@SuppressWarnings("rawtypes")
		List<NewStore> employeesList=getEmployeesList();
		System.out.println(employeesList.size());
		for(int i=0;i<employeesList.size();i++) {
			HSSFRow content=sheet.createRow(i+1);
			NewStore<?> employees=employeesList.get(i);
			content.createCell(0).setCellValue(employees.getStoreName());
			content.createCell(1).setCellValue(employees.getStoreCode());
			content.createCell(2).setCellValue(employees.getExternalCode());
			content.createCell(3).setCellValue(employees.getStores());
			content.createCell(4).setCellValue(employees.getInventory());
			content.createCell(5).setCellValue(employees.getProvince());
			content.createCell(6).setCellValue(employees.getCity());
			content.createCell(7).setCellValue(employees.getDirect());
			content.createCell(8).setCellValue(employees.getAddress());
			content.createCell(9).setCellValue(employees.getContactPerson());
			content.createCell(10).setCellValue(employees.getContactPhone());
			content.createCell(11).setCellValue(employees.getPostalCode());
			content.createCell(12).setCellValue(employees.getLongitude());
			content.createCell(13).setCellValue(employees.getLatitude());
				
		}
		
		try {
			FileOutputStream fileOutputStream=new FileOutputStream("D:/newstore/info/info.xlsx");
			workbook.write(fileOutputStream);
			fileOutputStream.close();
			System.out.println("导出成功!!!");
			}catch (IOException e){
				e.printStackTrace();
				System.out.println("导出失败!");
			}
		workbook.close();
		
	}

}
发布了6 篇原创文章 · 获赞 0 · 访问量 84

猜你喜欢

转载自blog.csdn.net/gaoxuetest/article/details/103908309