js导出excel,网页数据怎么转换成excel

  • Excel转换
  • 2024-01-05

js导出excel?我建议你还是通过后台来处理,用JS的话,客户端压力太大,容易导致内存溢出,浏览器崩溃。我用Java语言,通过jxl以及poi两种API给你写了例子,分别是用jxl读写excel文件,用poi读写excel文件。希望对你有帮助。那么,js导出excel?一起来了解一下吧。

js客户端生成excel

--------------------------------------------------------

我建议你还是通过后台来处理,用JS的话,客户端压力太大,容易导致内存溢出,浏览器崩溃。

我用Java语言,通过jxl以及poi两种API给你写了例子,分别是用jxl读写excel文件,用poi读写excel文件。希望对你有帮助。(需要jxl和poi的jar包)

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.format.Colour;

import jxl.format.UnderlineStyle;

import jxl.write.Label;

import jxl.write.WritableCellFormat;

import jxl.write.WritableFont;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelUtil {

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

String outFile = "D:/workspace/JavaStudy/src/util/excel/test.xls";

ExcelUtil.writeExcelByJXL(outFile, null);

}

/**

*

* @title: readExcelByJXL

* @description: 通过jxl读取excel文件

* @author yu ren tian

* @email yurentian@163.com

* @param excelFile

* @return

* @throws IOException

*/

private static List readExcelByJXL(String excelFile) throws IOException {

List rtn = new ArrayList();

FileInputStream fileInputStream = null;

try {

fileInputStream = new FileInputStream(excelFile);

Workbook excelWorkBook = Workbook.getWorkbook(fileInputStream);

Sheet sheet = excelWorkBook.getSheet(0);

int m = sheet.getRows();

int n = sheet.getColumns();

for (int i = 1; i < m; i++) {

Map map = new HashMap();

for (int j = 0; j < n; j++) {

Cell cell = sheet.getCell(j, i);

String cellContent = cell.getContents();

switch (j) {

case 0:

map.put("studentName", cellContent);

break;

case 1:

map.put("Chinese", cellContent);

break;

case 2:

map.put("Math", cellContent);

break;

case 3:

map.put("English", cellContent);

break;

case 4:

map.put("assess", cellContent);

break;

}

}

rtn.add(map);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (null != fileInputStream) {

fileInputStream.close();

}

return rtn;

}

}

/**

*

* @title: writeExcelByJXL

* @description: 通过jxl写入excel文件

* @author yu ren tian

* @email yurentian@163.com

* @param outFile

* @param list

* @throws IOException

*/

private static void writeExcelByJXL(String outFile, List list)

throws IOException {

WritableWorkbook wwb;

FileOutputStream fos;

try {

fos = new FileOutputStream(outFile);

// wwb = Workbook.createWorkbook(file);

wwb = Workbook.createWorkbook(fos);

WritableSheet sheet = wwb.createSheet("test", 0);

// 设置单元格的文字格式

WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,

WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,

Colour.BLUE);

WritableCellFormat wcf = new WritableCellFormat(wf);

//wcf.setBackground(Colour.GREEN);

wcf.setBackground(new CustomColor(11, "", 0, 0, 0));

for (int i = 0; i < 10; i++) {

Label label = new Label(i, 0, i + "", wcf);

sheet.addCell(label);

}

wwb.write();

wwb.close();

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

*

* @title: readExcelByPOI

* @description: 通过poi读取excel文件

* @author yu ren tian

* @email yurentian@163.com

* @param excelFile

* @return

* @throws IOException

*/

private static List readExcelByPOI(String excelFile) throws IOException {

List rtn = new ArrayList();

FileInputStream fin = null;

try {

fin = new FileInputStream(excelFile);

POIFSFileSystem fs = new POIFSFileSystem(fin);

HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFSheet sheet = wb.getSheetAt(0);

int m = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;

int n = 5;

for (int i = 1; i < m; i++) {

Map map = new HashMap();

for (int j = 0; j < n; j++) {

HSSFCell cell = sheet.getRow(i).getCell((short) j);

int type = cell.getCellType();

String cellContentString = null;

double cellContentDouble = 0;

if (type == 1) {

cellContentString = cell.getRichStringCellValue()

.getString();

System.out.println("cellContentString="

+ cellContentString);

} else if (type == 0) {

cellContentDouble = cell.getNumericCellValue();

System.out.println("cellContentDouble="

+ cellContentDouble);

}

System.out.println("j=" + j);

switch (j) {

case 0:

map.put("studentName", cellContentString);

break;

case 1:

map.put("Chinese", new Double(cellContentDouble));

break;

case 2:

map.put("Math", new Double(cellContentDouble));

break;

case 3:

map.put("English", new Double(cellContentDouble));

break;

case 4:

map.put("assess", cellContentString);

break;

}

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fin != null) {

fin.close();

}

return rtn;

}

}

/**

*

* @title: writeExcelByPOI

* @description: 通过poi写入excel

* @author yu ren tian

* @email yurentian@163.com

* @param outFile

* @param list

* @throws IOException

*/

private static void writeExcelByPOI(String outFile, List list)

throws IOException {

FileOutputStream fos = new FileOutputStream(outFile);

HSSFWorkbook wb = new HSSFWorkbook();

for (int sheetCount = 0; sheetCount < 5; sheetCount++) {

HSSFSheet sheet = wb.createSheet("组织" + (sheetCount + 1));

for (int rowCount = 0; rowCount < 10; rowCount++) {

for (int columnCount = 0; columnCount < 10; columnCount++) {

HSSFRow row = sheet.createRow(rowCount);

HSSFCell cell = row.createCell(new Short(columnCount + ""));

HSSFRichTextString richTextString = new HSSFRichTextString(

"行=" + rowCount + " 列=" + columnCount);

cell.setCellValue(richTextString);

}

}

}

wb.write(fos);

}

}

--------------------------------------------------------

如何在js文件中引入js文件

1、先封装好excel的java帮助类

2、点击jsp的导出按钮时,用同步请求带上参数调用后台方法

3、解析参数从数据库获取对于的表格数据

4、调用excel的java帮助类导出excel

js:

后台服务:

js导出文件

1. 调用后端接口导出文件

示例接口url https://gold-cdn.xitu.io/extension/0.3.9/package.crx

1.1 window.open(url)

会打开一个新窗口,开始后会自动关闭新窗口。Safair 后没有关闭新窗口。

Chrome、IE、Safair支持,貌似火狐不支持

1.2 window.location=url

在当前窗口

Chrome、Safair支持

1.3 iframe

在HTML中,iframe 的属性用src,但在JS中,只有部份浏览器支持修改src(读是没问题),真正通用的是要修改对应框架的href值。

1.4 点击链接

HTML5中给a标签增加了一个download属性,只要有这个属性,点击这个链接时浏览器就不在打开链接指向的文件,而是改为,目前只有chrome、firefox、opera、Edge支持。常用此方法点击图片。

IE既不支持a标签的download属性也不允许js调用a 标签的click方法。

js表格数据导出excel

表格数据导出到excel,我所知道的,有三种方法

1.最简单的就是导出csv,但不是直接导出excel:

这个不需要大的调整,跟着jsp页面的逻辑做出对应的csv文件,excel能够自动识别,再另存为xls/xlsx(csv:逗号分隔值,用纯文本形式储存表格数据)

2. 通过框架去生成excel表格,需要一定的编程能力:

不同的编程语言有不同做法

java:可以使用poi进行操作MS Office文档

.net:可通过DataGrid控件导出

3.通过报表框架来生成excel:

java:ireport、finereport等

vue导出excel

以下是个实例,请复制代码根据自身情况修改代码中相应内容:

const Excel = require('exceljs');

// 创建一个工作簿

const workbook = new Excel.Workbook();

// 创建一个工作表

const worksheet = workbook.addWorksheet('My Worksheet');

// 设置多级表头

worksheet.mergeCells('A1:C1');

worksheet.getCell('A1').value = 'Level 1';

worksheet.mergeCells('A2:B2');

worksheet.getCell('A2').value = 'Level 2 - A';

worksheet.getCell('C2').value = 'Level 2 - B';

// 写入数据

worksheet.getCell('A3').value = 'Data 1';

worksheet.getCell('B3').value = 'Data 2';

worksheet.getCell('C3').value = 'Data 3';

// 导出工作簿

workbook.xlsx.writeFile('example.xlsx')

.then(() => {

console.log('Excel file created successfully!');

});

以上就是js导出excel的全部内容,一安装vuejsonexcel npm install vuejsonexcel S 二mainjs里面引入并注册使用 import JsonExcel from #39vuejsonexcel#39Vuecomponent#39downloadExcel#39, JsonExcel三页面中使用 ltdownloadexcel #160 #。

猜你喜欢