对于需要纳入网盘管理的文件相当多,而且相当多,怎么办,本人使用Java写了一个工具,
只要设置一个文件目录后,自动导出文件目录清单(excel格式),
具体代码如下:
package pan;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class RocFileExcelMain {
private static final String TARGET_DIR="E:\BaiduNetdiskDownload\xxx";
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void main(String[] args) {
RocFileExcelMain main=new RocFileExcelMain();
main.doMainExcel();
}
//current row
private int currentRow=0;
private int currentCol=0;
public void doMainExcel(){
File dir = new File(TARGET_DIR); // 被遍历的目录。//E:\\Java_Code
String path4dir=dir.getPath();
String fileName4dir = path4dir.substring(path4dir.lastIndexOf("\\")+1);
File newFile = createNewFile(fileName4dir);
// 新文件写入数据,并下载*****************************************************
InputStream is = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
try {
is = new FileInputStream(newFile);// 将excel文件转为输入流
workbook = new XSSFWorkbook(is);// 创建个workbook,
//获取第一个sheet
sheet = workbook.getSheetAt(0);
} catch (Exception e1) {
e1.printStackTrace();
}
if (sheet != null) {
try {
// 写数据
FileOutputStream fos = new FileOutputStream(newFile);
//空三行
XSSFRow row = sheet.getRow(3);
if (row == null) {
row = sheet.createRow(3);
}
XSSFCell cell = row.getCell(0);
if (cell == null) {
cell = row.createCell(0);
}
XSSFCellStyle cellStyle=workbook.createCellStyle();
cellStyle.setWrapText(true);
print(sheet,cellStyle,dir,0);
/* 定义一个list集合假数据
List<Map<String, Object>> lst = new ArrayList();
Map<String, Object> map1 = new HashMap<String, Object>();
for (int i = 0; i < 42; i++) {
map1.put("id" + i, i);
lst.add(map1);
}
*/
//最后写入文件中
workbook.write(fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 删除创建的新文件
// this.deleteFile(newFile);
}
/*
for (int m = 0; m < lst.size(); m++) {
Map<String, Object> map = lst.get(m);
row = sheet.createRow((int) m + 3);
for (int i = 0; i < 42; i++) {
String value = map.get("id" + m) + "";
if (value.equals("null")) {
value = "0";
}
cell = row.createCell(i);
cell.setCellValue(value);
}
}
*/
public void print(XSSFSheet sheet,XSSFCellStyle cellStyle,File f, int level) {
StringBuffer sf=new StringBuffer();
for (int i = 0; i < level; i++) {
//System.out.print(" ");
sf.append(" ");
}
//System.out.println(f.getName());
currentRow++;
XSSFRow row = sheet.createRow((int) currentRow + 3);
if (f.isDirectory()) {//目录
XSSFCell cell = row.createCell(0);cell.setCellValue("目录");
String path=f.getPath();
String fileName = path.substring(path.lastIndexOf("\\")+1);
currentCol++;
if(level>0){
XSSFCell cellN = row.createCell(level);cellN.setCellStyle(cellStyle); cellN.setCellValue(fileName);
//sf.append(level).append("级目录:").append(f.getName()).append(LINE_SEPARATOR);
}else{//设置标题
sheet.getRow(0).getCell(0).setCellValue("文件清单【"+fileName+"】");
}
//
//迭代后续
for (File subF : f.listFiles()) {
print(sheet,cellStyle,subF, level + 1);
}
}else{//文件
XSSFCell cell = row.createCell(0);cell.setCellValue("文件");
//sf.append(level).append("级文件:").append(f.getName()).append(LINE_SEPARATOR);
XSSFCell cellN = row.createCell(level);cellN.setCellStyle(cellStyle);cellN.setCellValue(f.getName());
}
}
/**
* 复制文件
*
* @param s
* 源文件
* @param t
* 复制到的新文件
*/
public void fileChannelCopy(File s, File t) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(s), 1024);
out = new BufferedOutputStream(new FileOutputStream(t), 1024);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* two parent level directory to find sisp
* /D:/java/eclipse_4.3/workspace/rocpan/bin/
* D:/java/eclipse_4.3/workspace/rocpan/
* @return
*/
private String getSispPath() {
String classPaths = RocFileExcelMain.class.getResource("/").getPath();
String[] aa = classPaths.split("/");
String sispPath = "";
for (int i = 1; i < aa.length - 1; i++) {
sispPath += aa[i] + "/";
}
return sispPath;
}
/**
* 读取excel模板,并复制到新文件中供写入和下载
*
* @return
*/
public File createNewFile(String fileName4dir) {
// 读取模板,并赋值到新文件************************************************************
// 文件模板路径
String temp4path = (getSispPath() + "templateExcel/temp_pan.xlsx");
File temp4file = new File(temp4path);
// 保存文件的路径
String targetDir = TARGET_DIR;
File dir = new File(targetDir);
String dir4parent=dir.getParentFile().getPath();
// 判断路径是否存在
if (!dir.exists()) {
dir.mkdirs();
}
// 新的文件名
String newFileName = fileName4dir+"_文件清单.xlsx";
// 写入到新的excel
File newFile = new File(dir4parent, newFileName);
try {
newFile.createNewFile();
// 复制模板到新文件
fileChannelCopy(temp4file, newFile);
} catch (Exception e) {
e.printStackTrace();
}
return newFile;
}
/**
* 下载成功后删除
*
* @param files
*/
private void deleteFile(File... files) {
for (File file : files) {
if (file.exists()) {
file.delete();
}
}
}
}