import pandas as pd import xlwt # 对xls文件进行改写 path = r'F:\UserYan\testfile\ts.xlsx' path1 = r'F:\UserYan\testfile\cjia.xlsx' # 读取Excel文件 df = pd.read_excel(path,header=0).fillna({'价格':15,'单位':'个'}) df1 = pd.read_excel(path1,header=0) wj =pd.merge(df1,df,how='left',left_on='id',right_on='id') data = wj.values # 二维数组转excel储存 def transformation(col,data): workbook = xlwt.Workbook(encoding='utf-8',style_compression=0) sheet = workbook.add_sheet("Sheet") for i in range(len(col)): sheet.write(0, i, col[i]) for i in range(1, len(data) + 1, 1): for j in range(len(data[i - 1])): sheet.write(i, j, data[i - 1][j]) workbook.save(r'F:\UserYan\testfile\hb.xlsx') print("保存成功") col = ('id', '名称', '生产地', '价格', '单位', '数量', '总价') transformation(col,data) 这个功能用java怎么实现
时间: 2023-12-07 10:03:18 浏览: 155
import pandas as pd.docx
这个功能用Java可以通过Apache POI库实现。下面是Java代码实现:
```java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class ExcelUtils {
public static void writeToExcel(String[] col, Object[][] data, String filePath) {
Workbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet) workbook.createSheet("Sheet");
//设置列名的样式
Font font = workbook.createFont();
font.setBold(true);
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFont(font);
//写入列名
HSSFRow row = sheet.createRow(0);
for(int i = 0; i < col.length; i++) {
row.createCell(i).setCellValue(col[i]);
row.getCell(i).setCellStyle(style);
}
//写入数据
for(int i = 0; i < data.length; i++) {
row = sheet.createRow(i+1);
for(int j = 0; j < data[i].length; j++) {
row.createCell(j).setCellValue(data[i][j].toString());
}
}
//保存文件
try {
FileOutputStream fos = new FileOutputStream(new File(filePath));
workbook.write(fos);
fos.close();
System.out.println("保存成功");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String path = "F:\\UserYan\\testfile\\ts.xlsx";
String path1 = "F:\\UserYan\\testfile\\cjia.xlsx";
String outputPath = "F:\\UserYan\\testfile\\hb.xlsx";
//读取Excel文件
Object[][] data = null;
try {
Workbook wb1 = WorkbookFactory.create(new File(path));
Workbook wb2 = WorkbookFactory.create(new File(path1));
//左连接
//注意:POI中的行和列索引都是从0开始的,所以这里的id列索引需要减1
data = ExcelUtils.merge(wb1.getSheetAt(0), wb2.getSheetAt(0), 0, 0, 6);
} catch (IOException e) {
e.printStackTrace();
}
//写入Excel文件
String[] col = {"id", "名称", "生产地", "价格", "单位", "数量", "总价"};
ExcelUtils.writeToExcel(col, data, outputPath);
}
public static Object[][] merge(HSSFSheet sheet1, HSSFSheet sheet2, int idIndex1, int idIndex2, int columns) {
//获取行数和列数
int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum() + 1;
int rowCount2 = sheet2.getLastRowNum() - sheet2.getFirstRowNum() + 1;
//定义一个二维数组,用于存储连接后的数据
Object[][] data = new Object[rowCount2][columns];
int k = 0;
//遍历sheet2的每一行
for(int i = 0; i < rowCount2; i++) {
HSSFRow row2 = sheet2.getRow(i);
Object[] rowData = new Object[columns];
//将当前行的数据复制到rowData中
for(int j = 0; j < columns; j++) {
rowData[j] = row2.getCell(j).toString();
}
//在sheet1中查找id相同的行,并将它们合并
for(int j = 1; j < rowCount1; j++) {
HSSFRow row1 = sheet1.getRow(j);
if(row1.getCell(idIndex1).toString().equals(rowData[idIndex2].toString())) {
for(int l = 0; l < columns; l++) {
rowData[l] = (l == idIndex2) ? rowData[l] : row1.getCell(l).toString();
}
break;
}
}
//将合并后的数据存储到data中
data[k++] = rowData;
}
return data;
}
}
```
需要注意的是,这个Java代码中用到了Apache POI库,因此需要先将POI的jar包导入到项目中。
阅读全文