在从HTML导入时避免大数字的指数表示
Contents
[
Hide
]
有时,您的 HTML 中包含如 1234567890123456 这样超过 15 位数的数字,当您将 HTML 导入到 Excel 文件时,这些数字将转换为指数表示法,如 1.23457E+15。如果您希望您的数字被导入为原样,而不是转换为指数表示法,则请在加载 HTML 时使用 HtmlLoadOptions.KeepPrecision 属性,并将其设置为 true。
在从 HTML 导入时避免大数字的指数表示法
以下示例代码解释了 HtmlLoadOptions.KeepPrecision 属性的用法。它将以原样导入该数字,而不转换为指数表示法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(KeepPrecisionOfLargeNumbers.class) + "TechnicalArticles/"; | |
// Sample Html containing large number with digits greater than 15 | |
String html = "<html>" + "<body>" + "<p>1234567890123456</p>" + "</body>" + "</html>"; | |
// Convert Html to byte array | |
byte[] byteArray = html.getBytes(); | |
// Set Html load options and keep precision true | |
HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.HTML); | |
loadOptions.setKeepPrecision(true); | |
// Convert byte array into stream | |
java.io.ByteArrayInputStream stream = new java.io.ByteArrayInputStream(byteArray); | |
// Create workbook from stream with Html load options | |
Workbook workbook = new Workbook(stream, loadOptions); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Auto fit the sheet columns | |
worksheet.autoFitColumns(); | |
// Save the workbook | |
workbook.save(dataDir + "KPOfLargeNumbers_out.xlsx", SaveFormat.XLSX); | |
System.out.println("File saved"); |