我将一个包含下拉列表的单元格复制到另一个单元格,但是下拉列表没有被复制。如果我在excel文件中使用ctrl+c和ctrl+v进行复制,它会将下拉列表复制到新单元格中。
有没有办法用ApachePoi复制它?
我复制了包含下拉列表的单元格,但结果只是值而不是下拉列表。
我有一个工作表,我有下拉列表,并希望复制到另一个单元格和数据(Libro.xls)。
我在Excel类中有这个方法。
package es.fips;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class Excel {
private static HSSFWorkbook workbook = null;
private static FileInputStream fis = null;
private static HSSFSheet sheet = null;
public static boolean openExcel(String pathExcel) {
// Cargar la ruta del fichero Excel.
try {
fis = new FileInputStream(pathExcel);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
// Linkeamos el documento excel.
try {
workbook = new HSSFWorkbook(fis);
loadSheet(0);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static boolean loadSheet(int numHoja){
try {
// Obtenemos la hoja.
int hojas = workbook.getNumberOfSheets();
if((hojas-1) < numHoja){
throw new Exception("El fichero excel solo contiene "+hojas+" hojas.");
}else{
sheet = workbook.getSheetAt(numHoja);
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean saveChanges(String dirTemp, String name){
FileOutputStream fos = null;
// Crear fichero de salida para el nuevo excel.
try {
fos = new FileOutputStream(dirTemp+"/"+name);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
// Los cambios realizado en la plantilla los guardamos en el nuevo excel.
try {
workbook.write(fos);
fos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void closeExcel(){
try {
if (workbook != null) {
workbook.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copyCell() {
// Get the source / new row.
Row newRow = sheet.getRow(2);// Row number 3.
Row sourceRow = sheet.getRow(2);// Row number 3.
// Grab a copy of the old/new cell
Cell oldCell = sourceRow.getCell(3);// Column D.
Cell newCell = newRow.createCell(5);// Column F.
// Copy STYLE from old cell and apply to new cell.
HSSFCellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
newCell.setCellStyle(newCellStyle);
// If there is a cell comment, copy.
if (newCell.getCellComment() != null) {
newCell.setCellComment(oldCell.getCellComment());
}
// If there is a cell hyperlink, copy.
if (oldCell.getHyperlink() != null) {
newCell.setHyperlink(oldCell.getHyperlink());
}
// Set the cell data TYPE.
newCell.setCellType(oldCell.getCellType());
// Set the cell data value.
switch (oldCell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
newCell.setCellValue(oldCell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(oldCell.getStringCellValue());
break;
}
}
}
还有我的主职业:
package es.fips;
public class Main {
public static void main(String[] args) {
Excel.openExcel("Path/Libro.xls");
Excel.copyCell();
Excel.saveChanges("Path", "New.xls");
Excel.closeExcel();
}
}
结果(New.xls):仅复制单元格的值,而不复制下拉列表。
我想复制下拉列表,谢谢您以后的回答。
1条答案
按热度按时间whlutmcx1#
它与poi DataValidation函数相关。
在调用了copyCell()之后,你必须决定哪些单元格范围适用于哪些数据验证规则。