本文目录一览:

java 中jquery /js /其他方法 将统计表导入/出 excel 或 word

jquery和js都属于客户端脚本,不能直接执行excel或word。

你只能通过js的ajax方式将页面中的数据异步提交给java后台处理页中,然后由java来执行对excel或word数据的导入!在此js只能算是数据的搬运工,而真正做导入的事情还是有java这个后台语言来进行操作!

说这么多也就是想让你明白js是属于客户端脚本,它是不能操作excel或word的

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将excel数据导入到mongo数据库并更新数据

步骤一、将 *.xlsx 另存为 *.csv

步骤二、执行 imongoimport命令

mongoimport -d JinGuan -c 表 --type csv --headerline -file 文件.csv

参数说明:

d:数据库名

c:collection名

type:文件类型,指明是csv文件

headline:指明第一行是列名,不需要导入

file:csv文件路径及名字

更多参数请执行 mongoimport --help查看

如何实现SpreadJS的纯前端Excel导入导出

导入

导入时使用excelio的open方法,在successCallBack回掉中我们可以获取到Spread.Sheets的JSON对象。

导出

导出时使用save方法,传递json对象,在successCallBack中会获得一个Excel文件的blob对象。您可以在前段通过使用FileSaver.js直接保存Excel,也可将blob提交服务器处理。

具体的代码实现,请参考下面的博客

js中的数据怎样导出到excel

简单的办法:使用js生成一个table,可以直接复制到excel中,网上有很多表格插件

复杂的办法:js传递数据到服务器,服务器生成表格后返回一个下载链接

把sina专门的js服务器选择的数据导入到excel

1. 要正确的将Web客户端的Excel文件导入到服务器的数据库中,需要将客户端的Excel文件上传到服务器上。可以使用FileUpload控件完成。

2. Excel文件上传到服务器指定的目录中,这里假设是该站点的upfiles目录中。

3. 使用SQL语句从upfiles目录中的上传Excel文件中读取数据显示或写入数据库。

相关代码如下:

1. 前台文件:

%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudentInforInport.aspx.cs" Inherits="StudentInforInport" %

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""

html xmlns=""

head runat="server"

link rel="stylesheet" type="text/css" href="css/style.css"/

title从Excel表中导入学生数据/title

/head

body

form id="form1" runat="server"

div

table style="width: 96%; border-collapse: separate; text-align: center"

tr

td colspan="3"

从Excel中导入/td

/tr

tr

td colspan="3" style="text-align: left; height: 9px;"

/td

/tr

tr

td align="center" style="width: 20%;"

请选择Excel文件路径/td

td align="center" style="width: 483px; height: 18px; text-align: left"

asp:FileUpload ID="FileUpload1" runat="server" Width="555px" //td

td align="center" style="width: 10%"

asp:Button ID="Btn_Inport" runat="server" Text="导 入" OnClick="Btn_Inport_Click" //td

/tr

tr

td align="center"

请选择表名/td

td align="center" style="width: 483px; height: 18px; text-align: left"

asp:DropDownList ID="DDList_Sheet" runat="server"/asp:DropDownList/td

td align="center"

/td

/tr

tr

td colspan="3"

asp:GridView ID="GV_Excel" runat="server" Height="133px" Width="100%"

/asp:GridView

/td

/tr

tr

td style="height: 18px"

/td

td style="width: 483px; height: 18px;"

/td

td style="width: 243px; height: 18px;"

/td

/tr

/table

/div

/form

/body

/html

2. 后台代码:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

using System.Data.SqlClient;

public partial class StudentInforInport : System.Web.UI.Page

{

string strConn = System.Configuration.ConfigurationManager.AppSettings["strconn"].ToString().Trim(); //链接SQL数据库

protected void Page_Load(object sender, EventArgs e)

{

}

/// summary

/// 查询EXCEL电子表格添加到DATASET

/// /summary

/// param name="filenameurl"服务器路径/param

/// param name="table"表名/param

/// param name="SheetName"Sheet表名/param

/// returns读取的DataSet /returns

public DataSet ExecleDs(string filenameurl, string table, string SheetName)

{

string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0'";

OleDbConnection conn = new OleDbConnection(strConn);

conn.Open();

DataSet ds = new DataSet();

OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + SheetName + "]", conn);

odda.Fill(ds, table);

return ds;

}

protected void Btn_Inport_Click(object sender, EventArgs e)

{

if (FileUpload1.HasFile == false) //HasFile用来检查FileUpload是否有指定文件

{

Response.Write("scriptalert('请您选择Excel文件')/script ");

return; //当无文件时,返回

}

string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名

if (IsXls != ".xls")

{

Response.Write("scriptalert('只可以选择Excel文件')/script");

return; //当选择的不是Excel文件时,返回

}

string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + FileUpload1.FileName; // 获取Execle文件名 DateTime日期函数

string savePath = Server.MapPath(("~//upfiles//") + filename); //Server.MapPath 获得虚拟服务器相对路径

FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上

OperExcel Excel = new OperExcel();

ArrayList AL_ExcelSheet = new ArrayList();

AL_ExcelSheet = Excel.ExcelSheetName(savePath);

DDList_Sheet.Items.Clear();

for (int i = 0; i AL_ExcelSheet.Count; i++)

{

DDList_Sheet.Items.Add( AL_ExcelSheet[i].ToString() );

}

SqlConnection cn = new SqlConnection(strConn);

cn.Open();

DataSet ds = ExecleDs(savePath, filename, DDList_Sheet.Items[0].ToString()); //调用自定义方法得到数据

DataTable dt = ds.Tables[0];

if (dt.Rows.Count == 0)

{

Response.Write("scriptalert('Excel表为空表,无数据!')/script"); //当Excel表为空时,对用户进行提示

}

else

{

// 数据

GV_Excel.DataSource = dt;

GV_Excel.DataBind();

Response.Write("scriptalert('Excle表导入成功!');location='default.aspx'/script");

}

cn.Close();

}

}

注意:当导入的Excel文件中的内容很大时,将发生莫名的错误。因此导入的文件不能太大,一般少于5MB.