简体   繁体   English

使用HTML和JavaScript创建Excel格式输出

[英]Create Excel Format Output using HTML and JavaScript

I had interview and question asked was: 我接受了采访,问的问题是:

Write a JS plugin that can take cell and value as input and render excel format output on browser. 编写一个可以将单元格和值作为输入并在浏览器上呈现excel格式输出的JS插件。 For example, 例如,

Given Input (cell and value): 给定输入(单元格和值):

J3 = 5
A2 = 20
K1 = 10

Output on browser should be in excel format 浏览器上的输出应为Excel格式

   A   B   C ....... J    K .......
1                         10
2  20 
3                    5
..

I Was looking for correct solution for the problem. 我正在寻找问题的正确解决方案。

I tried solving this problem (writing psudeo code) 我尝试解决此问题(编写伪代码)

var  cell = {"J3": 5, "A2":20, "K1": 10}

// Function they will call for generate excel style table
generateExcel(cell, selector) {     
1.  create blank table  which has A-Z column (with selector as A-Z resp) and 1 to 100 rows (with selector as 1 to 100 resp)
2. Loop through each cell and for each cell
  2.1 find the column (J) and row (3)
  2.2 Add/replace value in that TD

3. Once all the information from cell in enter in table,  print the table in the document at given selector        

} }

They said it won't be efficient for huge number of cell inputs. 他们说,对于大量的细胞输入来说,效率不高。 I suggest that we can use Matrix create table 我建议我们可以使用矩阵创建表

   A   B...    J  K .... 
1 [               10      ]
2   20
3              5   

I think you started off well. 我想你起步很好。 Begin by creating a table that will contain the elements. 首先创建一个包含元素的表。 This will be 26 columns wide and as tall as the largest y value. 这将是26列宽,与最大y值一样高。 Convert the letters to numbers. 将字母转换为数字。

Sorry for w3schools link, I'm liable to get downvoted for even mentioning them, but they have the best laid out documentation on the table object that I could google for you. 对不起,w3schools链接,即使提到它们,我也很容易被打败,但它们具有关于表对象的最佳布局文档,我可以为您谷歌搜索。 I will update it if someone has something better. 如果有人有更好的东西,我会更新它。

http://www.w3schools.com/jsref/dom_obj_table.asp http://www.w3schools.com/jsref/dom_obj_table.asp

MDN Tutorial MDN教程

https://developer.mozilla.org/en/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces https://developer.mozilla.org/en/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces

You can then access the table cell most efficiently through 然后,您可以通过以下方式最有效地访问表格单元格

var table = ;//get by id or create element, not sure what they expect
table.rows[y].cells[x].appendChild(...);

Excel spreadsheets are tables. Excel电子表格是表格。 Can you use a simple table? 你可以用一个简单的桌子吗? If so, I would recommend the CSS border-collapse property to make it look better, as well as perhaps reducing cell padding and margin. 如果是这样,我建议使用CSS border-collapse属性使它看起来更好,以及减少单元格填充和边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM