gpt4 book ai didi

java - Excel - 自动输入到下一个空行

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:30 29 4
gpt4 key购买 nike

我正在开发一个程序,将特定数据存储在 Excel 存档中。我编写了一个代码,创建一个 Excel 文件、工作表并在其中写入。我的代码如下所示。问题是,在写入存档时,我必须指定应将信息插入到的行。无论如何,有没有办法让代码自动插入到下一个新的空行中?

package de.vogella.java.excel.writer;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class WriteExcel {
private WritableCellFormat timesBoldUnderline;
private WritableCellFormat times;
private String inputFile;

public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}

public void write() throws IOException, WriteException {
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close(); }
private void createLabel(WritableSheet sheet)
throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);

// create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);

CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);
// Write a few headers
addCaption(sheet, 0, 0, "type");
addCaption(sheet, 1, 0, "result");
addCaption(sheet, 2, 0, "time");
addCaption(sheet, 3, 0, "date");}
private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {
// Write a few number
for (int i = 1; i < 10; i++) {
// First column
addLabel(sheet, 0, i, "Boring text " + i);
// Second column
addNumber(sheet, 1, i, i * i);
// third column
addNumber(sheet, 2, i, i + 10);
//furth column
addNumber(sheet, 3, i, i * i);} }
private void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label); }
private void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException {
Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number); }
private void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);}
public static void main(String[] args) throws WriteException, IOException {
WriteExcel test = new WriteExcel();
test.setOutputFile("D:ghada.xls");
test.write();
System.out.println("Please check the result file under c:/temp/lars.xls "); }}

最佳答案

我没有使用过该API,所以不知道是否有更好的方法来做到这一点。另外,Java 不是我的专长,因此无法为您提供任何代码示例。

您需要找到电子表格中的第一个空行。假设 A 列始终已填充,您可以从第 1 行循环到第 N 行,直到找到空行。

您正在使用的 API 教程向您展示了如何读取 Excel 文件。

http://www.vogella.com/tutorials/JavaExcel/article.html

关于java - Excel - 自动输入到下一个空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36581953/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com