Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java POI-如何删除Excel中的特定字符串值?_Java_Apache Poi - Fatal编程技术网

Java POI-如何删除Excel中的特定字符串值?

Java POI-如何删除Excel中的特定字符串值?,java,apache-poi,Java,Apache Poi,我一直在使用ApachePOI以编程方式显示打印文件.xls格式。我遇到的唯一问题是,excel中的日期格式改为字符串格式,如下所示: 屏幕截图-excel文件 上面的excel图像包含一个(')字符串 这里的相关代码 cell = row.createCell((short)0); cell.setCellValue(form.getAdd2());// Date cell.setCellStyle(cellStyle2); 什么确切类型返回form.getAdd2()?我怀疑这是一个字符

我一直在使用ApachePOI以编程方式显示打印文件.xls格式。我遇到的唯一问题是,excel中的日期格式改为字符串格式,如下所示:

屏幕截图-excel文件

上面的excel图像包含一个(')字符串

这里的相关代码

cell = row.createCell((short)0);
cell.setCellValue(form.getAdd2());// Date
cell.setCellStyle(cellStyle2);

什么确切类型返回
form.getAdd2()
?我怀疑这是一个
字符串
,而不是
日期
。如果是这样,在设置单元格值之前,您需要将该
字符串
转换为
日期
。然后将该
Date
设置为单元格值

使用当前的
ApachePOI4.1.2
这可以通过
java.time.format.DateTimeFormatter
java.time.LocalDate
实现,因为现在已经有了

ApachePOI3.17
之前,可以使用
java.text.simpleDataFormat
java.util.Date
完成此操作。是当时使用的
setCellValue
方法

完整示例:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.List;
import java.util.ArrayList;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

class CreateExcelSetDateCellValue {

 public static void main(String[] args) throws Exception {

  List<Form> forms = new ArrayList<Form>();
  Form f;
  f = new Form(); f.setAdd2("10-Jan-2020"); forms.add(f);
  f = new Form(); f.setAdd2("1-Jan-2020"); forms.add(f);
  f = new Form(); f.setAdd2("12-Jan-2020"); forms.add(f);
  f = new Form(); f.setAdd2("19-Jan-2020"); forms.add(f);

  DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.US);
  //SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US); // up to apache poi 3.17

  Workbook workbook  = new XSSFWorkbook();
  CellStyle cellStyle2 = workbook.createCellStyle();
  cellStyle2.setDataFormat(workbook.createDataFormat().getFormat("d-MMM-yyyy"));

  Sheet sheet = workbook.createSheet();

  Row row = sheet.createRow(0); 
  Cell cell = row.createCell(0);
  cell.setCellValue("DateString");
  cell = row.createCell(1);
  cell.setCellValue("Date");


  int r = 1;
  for (Form form : forms) {
   row = sheet.createRow(r);
   cell = row.createCell(0);
   cell.setCellValue(form.getAdd2());// String
   cell.setCellStyle(cellStyle2);
   cell = row.createCell(1);
   cell.setCellValue(LocalDate.parse(form.getAdd2(), dateTimeFormatter));// Date
   //cell.setCellValue(simpleDateFormatter.parse(form.getAdd2()));// Date up to apache poi 3.17
   cell.setCellStyle(cellStyle2);
   r++;
  }

  sheet.setColumnWidth(0, 15*256);
  sheet.setColumnWidth(1, 15*256);

  FileOutputStream out = new FileOutputStream("Excel.xlsx");
  workbook.write(out);
  out.close();
  workbook.close();
 }

 static class Form {
  private String add2 = "";

  public void setAdd2(String add2) {
   this.add2 = add2;
  }

  public String getAdd2() {
   return this.add2;
  }
 }
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入java.util.List;
导入java.util.ArrayList;
导入java.time.format.DateTimeFormatter;
导入java.time.LocalDate;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Locale;
类CreateExcelSetDateCellValue{
公共静态void main(字符串[]args)引发异常{
列表表单=新的ArrayList();
表格f;
f=新表格();f.setAdd2(“2020年1月10日”);表格添加(f);
f=新表格();f.setAdd2(“2020年1月1日”);表格。添加(f);
f=新表格();f.setAdd2(“2020年1月12日”);表格。添加(f);
f=新表格();f.setAdd2(“2020年1月19日”);表格添加(f);
DateTimeFormatter DateTimeFormatter=模式的DateTimeFormatter.of(“d-MMM-yyyy”,Locale.US);
//SimpleDataFormat SimpleDataFormatter=新的SimpleDataFormat(“dd-MMM-yyyy”,Locale.US);//最高达apache poi 3.17
工作簿=新的XSSF工作簿();
CellStyle cellStyle2=workbook.createCellStyle();
cellStyle2.setDataFormat(工作簿.createDataFormat().getFormat(“d-MMM-yyyy”);
工作表=工作簿.createSheet();
Row Row=sheet.createRow(0);
Cell Cell=row.createCell(0);
cell.setCellValue(“日期字符串”);
cell=row.createCell(1);
cell.setCellValue(“日期”);
int r=1;
for(表格:表格){
行=表。创建行(r);
cell=row.createCell(0);
cell.setCellValue(form.getAdd2());//字符串
cell.setCellStyle(cellStyle2);
cell=row.createCell(1);
cell.setCellValue(LocalDate.parse(form.getAdd2(),dateTimeFormatter));//日期
//cell.setCellValue(simpleDataFormatter.parse(form.getAdd2());//最新apache poi 3.17
cell.setCellStyle(cellStyle2);
r++;
}
表.设置列宽(0,15*256);
页。设置柱宽(1,15*256);
FileOutputStream out=新的FileOutputStream(“Excel.xlsx”);
练习册。写(出);
out.close();
workbook.close();
}
静态类形式{
私有字符串add2=“”;
公共void setAdd2(字符串add2){
this.add2=add2;
}
公共字符串getAdd2(){
返回此文件。add2;
}
}
}
A
字符串形式包含日期,并显示您的问题。列
B
包含实际日期