如何更改导出的SheetJS xlsx文件的Excel表格列的类型?

huangapple go评论70阅读模式
英文:

How to change the type of columns of excel files for exported SheetJS xlsx?

问题

我想将一个表格转换成xlsx文件。我正在使用SheetJS。它可以成功获取数据,创建文件并下载。

问题出在Excel的数据类型上。在Excel中,所有列都被视为简单的字符串,例如23,0€不会被识别为货币,因此无法进行基本的Excel计算,比如sum

如何更改导出的SheetJS xlsx文件的Excel表格列的类型?

要在Excel中解决这个问题,你应该启用修改,尝试编辑然后按Enter键,让它被识别为货币单元格:

如何更改导出的SheetJS xlsx文件的Excel表格列的类型?

我的代码在这里(我无法直接添加代码段到问题中,因为它需要下载)。

我所有的Excel操作:

const worksheet = XLSX.utils.table_to_sheet(document.getElementById("myTable"));
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Worksheet");
XLSX.writeFile(workbook, "Export.xlsx");

我应该如何在SheetJS中更改类型以让Excel识别数值呢?

英文:

I want to convert a table into an xlsx file. I'm using SheetJS. It success to get data, create the file and download it.

The issue comes from excel types. For excel, all columns are simple string, and for example 23,0€ isn't recognize as money, and I can't do basic excel calcul as sum :

如何更改导出的SheetJS xlsx文件的Excel表格列的类型?

To fix it in excel, I should enable modification, try to edit then use enter to let it be recognized as money cell :

如何更改导出的SheetJS xlsx文件的Excel表格列的类型?

My code is here (I can't add direct snippet into question as it require download).

All my excel manipulation:

const worksheet = XLSX.utils.table_to_sheet(document.getElementById("myTable"));
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Worksheet");
XLSX.writeFile(workbook, "Export.xlsx");

How can I change type in SheetJS to let excel recognize values?

答案1

得分: 0

  1. 获取单元格类型。在创建真正的Excel文件(没有问题)后,你可以使用以下代码来获取单元格类型:
var XLSX = require("xlsx");
var wb = XLSX.readFile("export.xlsx", {cellNF: true});
var ws = wb.Sheets[wb.SheetNames[0]];
console.log(ws["A1"].z);

你将看到单元格的类型。在我的情况下,它是 '#,##0.00\ "€";[Red]-#,##0.00\ "€"'

  1. 对于每个 <td>,你应该 添加给定的格式,如下所示:
<td data-v="100" data-t="n" data-z='#,##0.00\ "€";[Red]-#,##0.00\ "€"'><p>100,00€</p></td>
  • data-v 包含值,例如,在我的情况下是整数(不包括
  • data-t 设置为 "n",因为该值是一个数字
  • data-z 是之前获得的单元格格式
  1. 使用相同的代码来导出内容,一切都会正常工作。
英文:

I found the answer. I should override set the cell-type myself, but as it's not a general format, it's different. (example of the default one)

  1. Get the cell type. After making the real excel file (without issue) you can use:
var XLSX = require(&quot;xlsx&quot;);
var wb = XLSX.readFile(&quot;export.xlsx&quot;, {cellNF: true})
var ws = wb.Sheets[wb.SheetNames[0]]
console.log(ws[&quot;A1&quot;].z)

And you will see the type. In my case, it's &#39;#,##0.00\\ &quot;€&quot;;[Red]\\-#,##0.00\\ &quot;€&quot;&#39;.

  1. For each &lt;td&gt;, you should add the given format everything as :
&lt;td data-v=&quot;100&quot; data-t=&quot;n&quot; data-z=&#39;#,##0.00\ &quot;€&quot;;[Red]\-#,##0.00\ &quot;€&quot;&#39;&gt;&lt;p&gt;100,00€&lt;/p&gt;&lt;/td&gt;
  • data-v will contains the value as, in my case, integer (without )
  • data-n is set as "n" because the value is a number
  • data-z is what we get before: the formatting of the cell
  1. Use same code to export the content, and everything will works.

huangapple
  • 本文由 发表于 2023年2月14日 21:22:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定