【问题标题】:How to store string as a currency in excel from java?java - 如何在java中将字符串存储为excel中的货币?
【发布时间】:2022-08-17 19:14:16
【问题描述】:

我想在从 java 导出到 excel 时将 \"$122.35\"(string) 转换为 \"$122.35\"(currency type)。 以下是代码sn-p:

    HSSFDataFormat df = (HSSFDataFormat) wb.createDataFormat();
                                CellStyle cs = wb.createCellStyle();
                                HSSFCellStyle ct = (HSSFCellStyle) wb.createCellStyle();
                                ct.setDataFormat((short) 7);
                                ct.setAlignment(HorizontalAlignment.RIGHT);
                                cell.setCellStyle(ct);
                                cell.setCellValue(viewCell.getValue());
 // in viewCell.getvalue the currency value is stored 
                                System.out.println(\"value: \");
                                continue;

标签: java excel apache-poi


【解决方案1】:

如果 Excel 将文本存储为单元格的值,则应用的数字格式根本无关紧要。所以如果要考虑数字格式,那么单元格值需要是数字的。

如果您只有 sring 格式的所有值,那么您至少需要知道其中哪些是真正的数字类型。在表中,大多数情况下,一列中的所有值都共享相同的类型。因此可以通过列号检测类型。

将字符串转换为数字的任务,可以使用 java.time.format.DateTimeFormatter 对应日期和 java.text.DecimalFormat 对应数字来完成。

完整的示例进行测试:

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class CreateExcel {
    
 static String[][] sheetData = new String[][] {
  new String[]{"Name", "Date", "Amount"},
  new String[]{"John", "2022-08-17", "$234.56"},
  new String[]{"Jane", "2022-09-15", "$1,234.56"},
  new String[]{"Mary", "2022-07-01", "$1,234,567.89"},   
  new String[]{"Claire", "2022-06-15", "-$1,234,567.89"},   
  new String[]{"Heidi", "2022-05-01", "-$4,567.89"},   
  new String[]{"Ahmed", "2022-04-15", "-$567.89"}  
 };
 
 public static void main(String[] args) throws Exception {
     
  DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  //decimalFormat = (DecimalFormat)NumberFormat.getCurrencyInstance(new Locale("en", "US"));
  DecimalFormat decimalFormat = new DecimalFormat("\u00A4#,##0.00", new DecimalFormatSymbols(new Locale("en", "US")));

  Workbook workbook = new XSSFWorkbook(); String filePath="./ExcelExample.xlsx";
  //Workbook workbook = new HSSFWorkbook(); String filePath="./ExcelExample.xls";
  
  CreationHelper creationHelper = workbook.getCreationHelper();
  CellStyle dateCellStyle = workbook.createCellStyle();
  dateCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd"));
  CellStyle currencyCellStyle = workbook.createCellStyle();
  currencyCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("$#,##0.00"));
  
  Sheet sheet = workbook.createSheet();
  for (int r = 0; r < sheetData.length; r++) {
   String[] rowData = sheetData[r];
   Row row = sheet.createRow(r);
   for (int c = 0; c < rowData.length; c++) {
    String cellValue = rowData[c];
    Cell cell = row.createCell(c);
    if (r == 0) {
     cell.setCellValue(cellValue);        
    } else if (c == 0) {
     cell.setCellValue(cellValue);
    } else if (c == 1) {
     LocalDate localDate = LocalDate.parse(cellValue, dateTimeFormatter);   
     cell.setCellValue(localDate);
     cell.setCellStyle(dateCellStyle);   
    } else if (c == 2) {
     Number number = decimalFormat.parse(cellValue);
     cell.setCellValue(number.doubleValue());
     cell.setCellStyle(currencyCellStyle);   
    }    
   }      
  }

  sheet.autoSizeColumn(0);
  sheet.autoSizeColumn(1);
  sheet.autoSizeColumn(2);

  try (FileOutputStream out = new FileOutputStream(filePath)) {
   workbook.write(out);
   workbook.close();
  }
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2020-10-20
    • 2010-10-17
    • 2019-06-23
    相关资源
    最近更新 更多