public class excel_r_or_w {
public static void main(String[] args) {
exportexcel();
}
//从excel获取值
public static Map<String, Map<String, Object>> getexcel() {
String path = "C:/Users/Desktop/xxxxx.xlsx";//导入excel文件路径及文件名
//summap用于保存从excel获取的所有数据
Map<String, Map<String, Object>> summap = new LinkedHashMap<String, Map<String, Object>>();
//map用于保存当前行的数据
Map<String,Object> map = new HashMap<String,Object>();
try {
//读取 xls/xlsx 创建excel工作簿
FileInputStream excelFileInputStream = new FileInputStream(path);
XSSFWorkbook workbook = new XSSFWorkbook(excelFileInputStream);
XSSFSheet sheet = workbook.getSheetAt(workbook.getNumberOfSheets()-1);
//用于记录当前行的id
int index = 0;
for (int rowIndex = 4; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
// XSSFRow 代表当前行数据
XSSFRow row = sheet.getRow(rowIndex);
if (row == null) {
continue;
}
//设置列数据类型
XSSFCell Cell1 = row.getCell(0);
Cell1.setCellType(Cell.CELL_TYPE_STRING);
XSSFCell Cell2 = row.getCell(1);
Cell2.setCellType(Cell.CELL_TYPE_NUMERIC);
XSSFCell Cell3 = row.getCell(2);
Cell3.setCellType(Cell.CELL_TYPE_NUMERIC);
//把当前行数据保存到map
map.put("Cell1", Cell1.getStringCellValue());
map.put("Cell2", Cell2.getNumericCellValue());
map.put("Cell3", Cell3.getNumericCellValue());
//保存map到summap中 以行id为key (copy()用来防止当前行数据被下一行的数据覆盖)
summap.put(String.valueOf(index), copy(map));
index = index + 1;
}
//用完要close workbook和excelFileInputStream
workbook.close();
excelFileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return summap ;
}
//导出数据到excel
public static void exportexcel(){
String filepath = "C:/Users/Otto/Desktop/excel/";
long currenttime = System.currentTimeMillis();
String filename = String.valueOf(currenttime);
File file=new File(filepath);
if (!file.exists()) {
file.mkdir();
}
File file1=new File(filepath + filename + "/");//导出excel的文件路径 如:C:/Users/Otto/Desktop/excel/时间/xxxxx.xls
if(!file1.exists()){
file1.mkdir();
}
filename = filepath + filename + "/";
int indexrow = 0;
int count = 0;
//声明一个工作簿
HSSFWorkbook wb = new HSSFWorkbook();
//声明表
HSSFSheet sheet = wb.createSheet("sheet");
//表格设置
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//创建表格头
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("Cell1");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("Cell2");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("Cell3");
cell.setCellStyle(style);
try {
//设置导出地址
FileOutputStream out = new FileOutputStream(filename + String.valueOf(currenttime) + ".xlsx");
//插入数据到excel
for (Map.Entry<String, Map<String, Object>> smap : getexcel().entrySet()) { //读取getexcel里面获取到的数据 可以换成数据库查询结果
row = sheet.createRow(indexrow + 1);
row.createCell(0).setCellValue((String) smap.getValue().get("Cell1"));
row.createCell(1).setCellValue((Double) smap.getValue().get("Cell2"));
row.createCell(2).setCellValue((Double) smap.getValue().get("Cell3"));
//row.createCell(3).setCellValue("这里放数据");
indexrow = indexrow + 1 ;
count = count + 1;
}
wb.write(out);
out.close();
wb.close();
System.out.println("导出成功!");
} catch (FileNotFoundException e) {
System.out.println("找不到路径 导出失败!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件流出错 导出失败!");
e.printStackTrace();
}
}
//防止数据被覆盖
public static Map<String, Object> copy(Map<String, Object> map){
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("Cell1", map.get("Cell1"));
resultMap.put("Cell2", map.get("Cell2"));
resultMap.put("Cell3", map.get("Cell3"));
return resultMap;
}
}
用的是poi的jar包 getexcel()方法读取 exportexcel()是生成 exportexcel()中的for循环可替换成其他 如:数据库读取的while循环