在java中写入excel中的特定单元格
如何使用ApachePOI将文本/图像写入特定的合并单元。特定单元格意味着,我直接将文本或图像写入B3:D7。下面的代码是每个索引的手动代码,而不是特定的单元格名称和编号。我想通过手机输入姓名和号码在java中写入excel中的特定单元格,java,excel,apache-poi,Java,Excel,Apache Poi,如何使用ApachePOI将文本/图像写入特定的合并单元。特定单元格意味着,我直接将文本或图像写入B3:D7。下面的代码是每个索引的手动代码,而不是特定的单元格名称和编号。我想通过手机输入姓名和号码 ClientAnchor anchor = helper.createClientAnchor(); //create an anchor with upper left cell _and_ bottom right cell anchor.setCol1(1); //C
ClientAnchor anchor = helper.createClientAnchor();
//create an anchor with upper left cell _and_ bottom right cell
anchor.setCol1(1); //Column B
anchor.setRow1(2); //Row 3
anchor.setCol2(2); //Column C
anchor.setRow2(3); //Row 4
您可以使用
sheet.addMergedRegion(新的CellRangeAddress(firstRow,lastRow,firstCol,lastCol))代码>用于创建合并区域。
为了获得索引,可以使用CellReference.convertColStringToIndex(“B”)
以获取列的索引。行的索引很简单,它只是编号-1
,例如,对于B3,索引是2
D3:G16的示例解决方案:
int firstRow = 2; // 3-1
int lastRow = 15; // 16-1
int firstCol = CellReference.convertColStringToIndex("D");
int lastCol = CellReference.convertColStringToIndex("G");
sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,firstCol,lastCol));
您可以使用sheet.addMergedRegion(新的CellRangeAddress(firstRow,lastRow,firstCol,lastCol))代码>用于创建合并区域。
为了获得索引,可以使用CellReference.convertColStringToIndex(“B”)
以获取列的索引。行的索引很简单,它只是编号-1
,例如,对于B3,索引是2
D3:G16的示例解决方案:
int firstRow = 2; // 3-1
int lastRow = 15; // 16-1
int firstCol = CellReference.convertColStringToIndex("D");
int lastCol = CellReference.convertColStringToIndex("G");
sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,firstCol,lastCol));
这里有一个例子
public class MMM {
static void mergeCells(XSSFSheet sheet, String cells) {
String regex = "([A-Z]+)(\\d+):([A-Z]+)(\\d+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cells);
if(matcher.matches()) {
int col1 = CellReference.convertColStringToIndex(matcher.group(1));
int col2 = CellReference.convertColStringToIndex(matcher.group(3));
int row1 = Integer.parseInt(matcher.group(2)) - 1;
int row2 = Integer.parseInt(matcher.group(4)) - 1;
sheet.addMergedRegion(new CellRangeAddress(row1, row2, col1, col2));
}
}
public static void main(String[] args) throws IOException {
OutputStream outputStream = new FileOutputStream("wwww2.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
mergeCells(sheet, "AAD10:ADD23");
workbook.write(outputStream);
outputStream.close();
}
}
这里有一个例子
public class MMM {
static void mergeCells(XSSFSheet sheet, String cells) {
String regex = "([A-Z]+)(\\d+):([A-Z]+)(\\d+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cells);
if(matcher.matches()) {
int col1 = CellReference.convertColStringToIndex(matcher.group(1));
int col2 = CellReference.convertColStringToIndex(matcher.group(3));
int row1 = Integer.parseInt(matcher.group(2)) - 1;
int row2 = Integer.parseInt(matcher.group(4)) - 1;
sheet.addMergedRegion(new CellRangeAddress(row1, row2, col1, col2));
}
}
public static void main(String[] args) throws IOException {
OutputStream outputStream = new FileOutputStream("wwww2.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
mergeCells(sheet, "AAD10:ADD23");
workbook.write(outputStream);
outputStream.close();
}
}
你能把全部代码放在这里吗。比如合并D3:G16。谢谢。你能把全部代码放在这里吗。比如合并D3:G16。谢谢。你能把全部代码放在这里吗。比如合并D3:G16。谢谢,谢谢你。在合并单元格中添加样式如何?例如,我会给它加一个背景色吗?谢谢。没错,我只是假设这个专栏是一个字符。您可以使用正则表达式来拆分单元格地址。但是,对于第1行和第2行,应该将其减去1,以获得特定的行。见下文。introw1=Integer.parseInt(matcher.group(2))-1;introw2=Integer.parseInt(matcher.group(4))-1;在合并单元格中添加新的单元格样式(如背景色)如何?谢谢。你能把全部代码放在这里吗。比如合并D3:G16。谢谢,谢谢你。在合并单元格中添加样式如何?例如,我会给它加一个背景色吗?谢谢。没错,我只是假设这个专栏是一个字符。您可以使用正则表达式来拆分单元格地址。但是,对于第1行和第2行,应该将其减去1,以获得特定的行。见下文。introw1=Integer.parseInt(matcher.group(2))-1;introw2=Integer.parseInt(matcher.group(4))-1;在合并单元格中添加新的单元格样式(如背景色)如何?谢谢