使用Python查找下一个空列以在Excel中写入查询数据(

问题描述 投票:0回答:2

我正在使用 pandas 从 Oracle 获取查询结果,我想将其写入 Excel 文件并将数据放入第一列为空,所以第一次应该是 A 列,下次我运行这个编程它应该将数据添加到 B 列等中。

我正在使用 openpyxl 使用我找到的 max_row / max_column 方法写入此数据。我已经搜索了一段时间,但找不到使用 openpyxl 在下一个空列中执行此操作的方法。

main_file = glob('C:\\Users\\dataTemplate.xlsx')[0]

nwb = load_workbook(main_file)
nws = nwb.worksheets[0]

copy_file = (
    r'C:\\Users\\queryData.xlsx')

cwb = load_workbook(copy_file)
cws = cwb.worksheets[0]

#Updated
nmc = nws.max_column + 1

mr = cws.max_row
mc = cws.max_column

for i in range(1, mr + 1):
    for j in range(1, mc + 1):
        c = cws.cell(row=i, column=j)
        nws.cell(row=i, column=nmc + j).value = c.value
python-3.x pandas openpyxl
2个回答
1
投票

更新

当您使用

pandas
时,您可以使用以下代码:

with pd.ExcelWriter('data.xlsx', engine='openpyxl', mode='a', if_sheet_exists='overlay') as writer:
    wb = writer.book
    ws = wb.active
    df.to_excel(writer, startrow=ws.min_row-1, startcol=ws.max_column, index=False)

旧答案

您可以使用

ws.max_column
ws.max_row
:

from openpyxl import load_workbook
from openpyxl.utils import get_column_letter

wb = load_workbook('test.xlsx')
ws = wb.active

输出:

>>> ws.max_row
5

>>> ws.max_column
9

>>> get_column_letter(ws.max_column)
'I'

我的Excel文件:


0
投票

公共类 ExcelUpdater {

public static void updateNextEmptyColumn(String filePath, String sheetName, String rowValue, String valueToUpdate) throws IOException {
    FileInputStream fileInputStream = new FileInputStream(filePath);
    Workbook workbook = new XSSFWorkbook(fileInputStream);
    Sheet sheet = workbook.getSheet(sheetName);

    int rowIndex = findRowIndexByValue(sheet, rowValue);
    if (rowIndex == -1) {
        // Row value not found, handle the error accordingly
        return;
    }

    Row row = sheet.getRow(rowIndex);
    int nextEmptyColumnIndex = row.getLastCellNum();

    Cell cell = row.createCell(nextEmptyColumnIndex);
    cell.setCellValue(valueToUpdate);

    FileOutputStream fileOutputStream = new FileOutputStream(filePath);
    workbook.write(fileOutputStream);

    fileOutputStream.close();
    workbook.close();
    fileInputStream.close();
}

private static int findRowIndexByValue(Sheet sheet, String rowValue) {
    for (Row row : sheet) {
        for (Cell cell : row) {
            if (cell.getCellType() == CellType.STRING && cell.getStringCellValue().equals(rowValue)) {
                return row.getRowNum();
            }
        }
    }
    return -1; // Row value not found
}

public static void main(String[] args) {
    String filePath = "path/to/your/excel/file.xlsx";
    String sheetName = "Sheet1";
    String rowValue = "RowValueToSearch";
    String valueToUpdate = "ValueToUpdate";

    try {
        updateNextEmptyColumn(filePath, sheetName, rowValue, valueToUpdate);
        System.out.println("Value updated successfully!");
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Error occurred: " + e.getMessage());
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.