javapoi设置时间空间_java-POI-如何将单元格值设置为Date并应用默认的Ex。。。
javapoi设置时间空间_java-POI-如何将单元格值设置为Date并
应⽤默认的Ex。。。
java - POI - 如何将单元格值设置为Date并应⽤默认的Excel⽇期格式?
我已经使⽤Apache POI⼀段时间以编程⽅式读取现有的Excel 2003⽂件。 现在我有了⼀个新的要求,即在内存中创建整个.xls⽂件(仍使⽤Apache POI),然后将它们写⼊⽂件末尾。 阻碍我的唯⼀问题是处理带⽇期的细胞。
请考虑以下代码:
Date myDate = new Date();
HSSFCell myCell;
// code that assigns a cell from an HSSFSheet to 'myCell' would
myCell.setCellValue(myDate);
当我将包含此单元格的⼯作簿写⼊⽂件并使⽤Excel打开时,单元格显⽰为数字。 是的,我确实认识到E
xcel将其“⽇期”存储为⾃1900年1⽉1⽇以来的天数,这就是单元格中的数字所代表的数字。
问题:我可以在POI中使⽤哪些API调⽤来告诉它我需要将默认⽇期格式应⽤于我的⽇期单元格?
理想情况下,如果⽤户在Excel中⼿动打开电⼦表格并键⼊Excel识别为⽇期的单元格值,我希望电⼦表格单元格显⽰的Excel默认⽇期格式与Excel分配的格式相同。
6个解决⽅案
145 votes
CellStyle cellStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
ipad wificellStyle.setDataFormat(
cell = ateCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
ninja answered 2019-09-10T03:53:03Z
中国历史朝代顺序21 votes
要设置为默认Excel类型⽇期(默认为操作系统级别区域设置/ - >例如,如果您在Excel的单元格格式选择器中选择它时,xlsx在德国或英国⼈打开时会显⽰不同/并且标有星号)您应该:
CellStyle cellStyle = ateCellStyle();
cellStyle.setDataFormat((short)14);
cell.setCellStyle(cellStyle);
林林总总什么意思我⽤xlsx做到了它并且⼯作正常。
BlondCode answered 2019-09-10T03:53:33Z
三月英语缩写11 votes
此⽰例适⽤于.xlsx⽂件类型。 此⽰例来⾃⽤于创建.xslx电⼦表格的.jsp页⾯。
import org.apache.poi.xssf.usermodel.*; //import needed
XSSFWorkbook wb = new XSSFWorkbook (); // Create workbook
XSSFSheet sheet = wb.createSheet(); // Create spreadsheet in workbook
XSSFRow row = ateRow(rowIndex); // Create the row in the spreadsheet
//1. Create the date cell style
XSSFCreationHelper createHelper = wb.getCreationHelper();
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
//2. Apply the Date cell style to a cell
//This example sets the first cell in the row using the date cell style
cell = ateCell(0);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
Mr. Port St Joe answered 2019-09-10T03:53:57Z
1 votes
我在这⾥写我的答案,因为它可能对其他读者有帮助,他们的要求可能与提问者的要求略有不同。
我准备⼀个.xlsx模板; 所有将填充⽇期的单元格已经格式化为⽇期单元格(使⽤Excel)。
我使⽤Apache POI打开.xlsx模板,然后只将⽇期写⼊单元格,它可以⼯作。
在下⾯的⽰例中,单元格A1已在Excel中格式化,格式为[$-409]mmm yyyy,Java代码仅⽤于填充单元格。FileInputStream inputStream = new FileInputStream(new File("Path to .xlsx template"));
Workbook wb = new XSSFWorkbook(inputStream);
Date date1=new Date();
Sheet xlsMainTable = (Sheet) wb.getSheetAt(0);
Row myRow= Row(0, xlsMainTable);
全国化妆品排行榜打开Excel时,⽇期格式正确。
gordon613 answered 2019-09-10T03:54:47Z
0 votes
此代码⽰例可⽤于更改⽇期格式。 在这⾥,我想从yyyy-MM-dd改为dd-MM-yyyy。 这⾥pos是列的位置。import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class Test{
public static void main( String[] args )
{
String input="D:\\somefolder\\somefile.xlsx";
String output="D:\\somefolder\\someoutfile.xlsx" FileInputStream file = new FileInputStream(new File(input)); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet = SheetAt(0);
Iterator iterator = sheet.iterator();
Cell cell = null;
Row row=null;
();
int pos=5; // 5th column is date.
while(iterator.hasNext())
{
();
Cell(pos-1);
//CellStyle cellStyle = wb.createCellStyle();
XSSFCellStyle cellStyle = (CellStyle(); CreationHelper createHelper = wb.getCreationHelper(); cellStyle.setDataFormat(
try {
d= sdf.StringCellValue());
} catch (ParseException e) {
// TODO Auto-generated catch block
d=null;
e.printStackTrace();
continue;
}
cell.setCellValue(d);
cell.setCellStyle(cellStyle);
}
file.close();
歌唱祖国的歌曲有哪些
FileOutputStream outFile =new FileOutputStream(new File(output));
workbook.write(outFile);
workbook.close();
outFile.close();
}}
Gaurav Khare answered 2019-09-10T03:55:11Z
0 votes
要知道Excel使⽤的格式字符串⽽不必猜测它:创建⼀个excel⽂件,在单元格A1中写⼊⽇期并根据需要对其进⾏格式化。 然后运⾏以下⾏:
FileInputStream fileIn = new FileInputStream("test.xlsx");
Workbook workbook = ate(fileIn);
CellStyle cellStyle = SheetAt(0).getRow(0).getCell(0).getCellStyle();
String styleString = DataFormatString();
System.out.println(styleString);
CellStyle cellStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
cellStyle.ateDataFormat().getFormat("d/m/;@"));
cell = ateCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
Luke answered 2019-09-10T03:55:42Z

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。