请帮我写一个若干Excel文件里筛选出含有某个名字的Excel文件并把它们放到一个文件夹里
时间: 2024-05-10 22:21:07 浏览: 14
以下是Python代码实现:
```
import os
import shutil
import glob
# 要查找的名字
name_to_find = '张三'
# 查找当前目录下所有的Excel文件
excel_files = glob.glob('*.xlsx')
# 创建存储筛选结果的文件夹
if not os.path.exists('筛选结果'):
os.mkdir('筛选结果')
# 遍历所有Excel文件,筛选出含有指定名字的文件,并将其复制到筛选结果文件夹中
for file in excel_files:
with open(file, 'r') as f:
contents = f.read()
if name_to_find in contents:
shutil.copy(file, os.path.join('筛选结果', file))
print(f'{file} 已复制到筛选结果文件夹中')
```
运行以上代码,就能在当前目录下找到一个名为“筛选结果”的文件夹,其中包含了所有含有指定名字的Excel文件。
相关问题
用java写一个降大文件excel分成若干份文件的工具类
好的,以下是一个用 Java 写的将大文件 Excel 分成若干份文件的工具类:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelSplitter {
private static final int DEFAULT_ROW_LIMIT = 10000;
public static void split(File inputFile, int rowLimit) throws IOException {
FileInputStream fis = new FileInputStream(inputFile);
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getLastRowNum() + 1;
int sheetCount = (int) Math.ceil((double) rowCount / rowLimit);
List<Sheet> sheets = new ArrayList<>(sheetCount);
for (int i = 0; i < sheetCount; i++) {
Sheet newSheet = workbook.createSheet("Sheet" + (i + 1));
sheets.add(newSheet);
}
int currentSheetIndex = 0;
int currentRowIndex = 0;
for (int i = 0; i < rowCount; i++) {
Row row = sheet.getRow(i);
if (currentRowIndex == rowLimit) {
currentSheetIndex++;
currentRowIndex = 0;
}
Row newRow = sheets.get(currentSheetIndex).createRow(currentRowIndex++);
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
Cell newCell = newRow.createCell(j);
if (cell != null) {
switch (cell.getCellType()) {
case BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case NUMERIC:
newCell.setCellValue(cell.getNumericCellValue());
break;
case STRING:
newCell.setCellValue(cell.getStringCellValue());
break;
case FORMULA:
newCell.setCellFormula(cell.getCellFormula());
break;
case BLANK:
newCell.setBlank();
break;
case ERROR:
newCell.setCellErrorValue(cell.getErrorCellValue());
break;
default:
break;
}
}
}
}
fis.close();
File outputDir = new File(inputFile.getParentFile(), "output");
if (!outputDir.exists()) {
outputDir.mkdir();
}
for (int i = 0; i < sheets.size(); i++) {
Sheet sheetToWrite = sheets.get(i);
File outputFile = new File(outputDir, "output" + (i + 1) + ".xlsx");
FileOutputStream fos = new FileOutputStream(outputFile);
workbook.write(fos);
fos.close();
}
workbook.close();
}
public static void split(File inputFile) throws IOException {
split(inputFile, DEFAULT_ROW_LIMIT);
}
}
```
这个工具类可以将一个 Excel 文件分成若干个文件,每个文件最多包含指定的行数。你可以使用以下代码来调用这个工具类:
```java
File inputFile = new File("input.xlsx");
ExcelSplitter.split(inputFile, 5000);
```
这个例子将会把 `input.xlsx` 文件分成若干个文件,每个文件最多包含 5000 行。分割后的文件将会保存在 `input.xlsx` 文件所在目录下的 `output` 文件夹中。
用Java 多线程写一个读写大文件excel分成若干份的工具类
可以使用Java的多线程技术来实现读写大文件excel分成若干份的工具类。具体实现步骤如下:
1. 首先,使用Java的IO流读取大文件excel,并将其分成若干份。
2. 然后,使用多线程技术对每一份excel进行读写操作,以提高程序的效率。
3. 在读写excel的过程中,需要使用Java的线程同步机制来保证数据的一致性和安全性。
4. 最后,将所有分割后的excel文件合并成一个完整的excel文件。
下面是一个简单的Java多线程读写大文件excel分成若干份的工具类示例:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelSplitter {
private static final int BUFFER_SIZE = 4096;
private static final int THREAD_COUNT = 4;
public static void splitExcel(File inputFile, int splitCount) throws IOException {
// 计算每个文件的大小
long fileSize = inputFile.length();
long splitSize = fileSize / splitCount;
// 创建分割后的文件列表
List<File> splitFiles = new ArrayList<>();
for (int i = 0; i < splitCount; i++) {
File splitFile = new File(inputFile.getParent(), inputFile.getName() + "." + i);
splitFiles.add(splitFile);
}
// 开启多线程读写excel
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < splitCount; i++) {
final int index = i;
Thread thread = new Thread(() -> {
try (FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(splitFiles.get(index))) {
// 定位到当前文件的起始位置
fis.skip(splitSize * index);
// 读取当前文件的数据
byte[] buffer = new byte[BUFFER_SIZE];
long readCount = 0;
int len;
while ((len = fis.read(buffer)) != -1 && readCount < splitSize) {
fos.write(buffer, 0, len);
readCount += len;
}
} catch (IOException e) {
e.printStackTrace();
}
});
threads.add(thread);
thread.start();
}
// 等待所有线程执行完毕
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 合并分割后的文件
try (FileOutputStream fos = new FileOutputStream(inputFile)) {
for (File splitFile : splitFiles) {
try (FileInputStream fis = new FileInputStream(splitFile)) {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
}
splitFile.delete();
}
}
}
}
```
使用示例:
```java
File inputFile = new File("large_excel_file.xlsx");
ExcelSplitter.splitExcel(inputFile, 4);
```
以上就是一个简单的Java多线程读写大文件excel分成若干份的工具类实现。