jexcel_修改单元格并同步数据库
aspx文件
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="有效代码_修改单元格并同步数据库.aspx.cs" Inherits="web_page_ssc_有效代码_刷新" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title></title> 9 <%--CSS--%> 10 <link href="/web_page_ssc/css/jexcel.css" rel="stylesheet" /> 11 <link href="/web_page_ssc/css/jsuites.css" rel="stylesheet" /> 12 <link href="/web_page_ssc/css/jexcel.datatables.css" rel="stylesheet" /> 13 14 <%--JS--%> 15 <script src="../web_page_ssc/js/jquery.min.js"></script> <%--重要**--%> 16 <%--Jexcel包--%> 17 <script src="../web_page_ssc/js/jexcel.js"></script> 18 <script src="../web_page_ssc/js/jsuites.js"></script> 19 20 21 </head> 22 <body> 23 <form id="form1" runat="server"> 24 25 <button id="my_refresh" type="button" class="btn btn-warning" > 26 <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>刷新 OK 27 </button> 28 <%--表格--%> 29 <div id="spreadsheet" class="container"></div> 30 </form> 31 32 </body> 33 34 <script type="text/javascript"> 35 36 37 //修改单元格并同步数据库 此代码必须在 var mySpreadsheet之前,否者不会出现alert()弹窗 38 var changed = function (instance, cell, x, y, value) { 39 var cellName = jexcel.getColumnNameFromId([0, y]); //当前行首列坐标 。 例如:A1 40 var DBID = document.getElementById('spreadsheet').jexcel.getValue(cellName); //获取DBid值 例如:5989 41 var modifyValue = value; //修改后的值 例如:S7160077 42 var rowData = document.getElementById('spreadsheet').jexcel.getRowData(y); //当前行的整行数据 43 if (x = 2) { var fieldName = "Shipto"; }; //字段名 44 45 46 //确认,是否拿到值 47 alert(cellName + '-' + DBID + '-' + modifyValue + '-' + fieldName); //ok 48 49 $.ajaxSetup({ 50 async: false 51 }) 52 53 $.ajax({ 54 url: "Handler2.ashx", //变更内容_写入数据库 55 datatype: "json", 56 data: { "RequestType": "save_data", "DBID": DBID, "modifyValue": modifyValue, "fieldName": fieldName }, 57 success: function (data) { 58 //注释:返回结果,此处不需要了 59 alert(data); 60 }, 61 error: function (error) { 62 alert(error.responseText); 63 } 64 }); 65 66 } 67 68 var mySpreadsheet = jspreadsheet(document.getElementById('spreadsheet'), { 69 70 url: '/web_page_ssc/Handler1.ashx', // 获取数据 71 search: true, // 搜索 搜索自定义的新事件 onsearchstart、onserchrow * 72 async: true, 73 allowExport: true, // 是否允许导出 74 colWidths: [50, 80, 100, 100, 100, 80, 100, 100, 80, 80, 100, 70, 70, 70, 70, 80, 100], //列宽 75 tableOverflow: true, // 是否允许表溢出溢出的时候右边有那个滚动条 76 tableWidth: "1520px", // 表宽度 77 tableHeight: "430px", // 表高度 78 pagination: 20, // 每页显示10行数据 * 79 paginationOptions: [10, 15, 20, 50], //用户自主选择每页需展示的数据条数 * 80 tabs: true, //标签 81 //toolbar: true, //工具栏 需加载<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" /> 目前使用不可 82 //toolbar: '#toolbar', //工具栏 83 editable: true, // 启用行内编辑 84 striped: true, //表格斑马纹 85 sortable: true, //排序 86 sortOrder: "asc", //排序方式 87 88 //列属性 OK 89 columns: [ 90 91 { field: 'id', title: 'DBid', sortable: true, align: 'center', }, 92 //{ field: 'checkbox', title: 'checkbox', sortable: true, align: 'center', type: 'checkbox', }, 93 { field: 'type', title: 'type', sortable: true, align: 'center', }, 94 { field: 'Shipto', title: 'Shipto', sortable: true, align: 'center', }, 95 { field: 'Soldto', title: 'Soldto', sortable: true, align: 'center', }, 96 { field: 'MOsPONo', title: 'MOsPONo', sortable: true, align: 'center', }, 97 { field: 'Item', title: 'Item', sortable: true, align: 'center', }, 98 { field: 'Docrecodedate', title: 'Docrecodedate', sortable: true, align: 'center', type: 'calendar', options: { format: 'YYYY/MM/DD' }, }, 99 { field: 'Firstdate', title: 'Firstdate', sortable: true, align: 'center', type: 'calendar', options: { format: 'YYYY/MM/DD' }, }, 100 101 ], 102 103 //事件 必须先完成事件的function*** 104 //onchanged: changed, //修改单元格并同步数据库 105 onchange: changed, //修改单元格并同步数据库 之前onchange输成了onchanged,不能出现alert()弹窗* 106 107 108 109 }); 110 111 //刷新_按钮点击事件 OK 112 $('#my_refresh').on('click', function () { 113 document.getElementById('spreadsheet').jexcel.refresh(); //(实现刷新)* 114 }); 115 116 117 </script> 118 119 </html>
Handler1.ashx文件
1 <%@ WebHandler Language="C#" Class="Handler1" %> 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Web; 7 using System.Data; 8 using System.Data.SqlClient; //数据库 9 using Newtonsoft.Json; //操作json库 10 using Newtonsoft.Json.Linq; 11 12 //Handler1 主要做数据库查询用 13 public class Handler1 : IHttpHandler 14 { 15 16 //HttpContext context 是ASP.NET中的一个对象,代表正在处理的HTTP请求的上下文信息,包括请求头、请求主体、响应头、响应主体等。这个对象在ASP.NET Web页面或者Web服务中经常用到,用于获取客户端提交的数据、生成动态HTML并将其发送回客户端等。 17 public void ProcessRequest(HttpContext context) 18 { 19 context.Response.ContentType = "text/plain"; 20 context.Response.Write(mysourcestring()); 21 22 //源数据 //获取或设置客户端使用的 HTTP 数据传输方法(GET 或 POST),"sourcedata"为自定义函数 23 //if (context.Request["RequestType"] == "sourcedata") 24 //{ 25 //int current_page = Convert.ToInt32(context.Request["current_page"]);//页码,第几页 26 //int my_rows = Convert.ToInt32(context.Request["page_rows"]);//行数,一页中显示多少行 27 //context.Response.Write(mysourcestring(current_page, my_rows)); //mysourcestring 是一个自定义的函数,接收两个参数:当前页码和每页的行数,根据这些参数计算出字符串并返回。 28 //int current_page = 1; 29 //int my_rows = 8; 30 //context.Response.Write(mysourcestring(current_page, my_rows)); //context是网页间传递参数的意思,不过context.Response.Write好像没见过,Response.Write就是输出数据 31 32 } 33 34 public bool IsReusable 35 { 36 get 37 { 38 return false; 39 } 40 } 41 42 //获取outstanding数据库数据 43 private string mysourcestring() 44 { 45 //返回的样式例 [{"id":5989,"type":"ZVDH","Shipto":"S716000456","Soldto":"S7160"},{"id":6003,"type":"ZPPH","Shipto":"S2000050","Soldto":"S2000"}] 46 string s0 = "select * from outstanding"; 47 SqlConnection conn = new SqlConnection("server=8.17.8.180;database=SSC;uid=***;pwd=***"); 48 conn.Open(); 49 SqlCommand cmd = new SqlCommand(s0, conn); //用来执行查询语句 50 SqlDataAdapter sda = new SqlDataAdapter(); //数据库适配器,用来充当数据库与数据集之间的桥梁 51 sda.SelectCommand = cmd; //选择命令向数据库发送(发送查询语句) 52 DataSet ds = new DataSet(); //创建一个数据集对象,相当于小型数据库,它当中存放若干个数据块 53 sda.Fill(ds, "cs"); //Fill: 填充 把数据填充小型数据库的“CS”表中。 54 System.Data.DataTable table1 = ds.Tables[0]; 55 56 string jsonstr = JsonConvert.SerializeObject(table1); //datatable格式转换Json格式 前提要导入 using Newtonsoft.Json 57 conn.Close(); 58 return jsonstr; 59 } 60 61 //搜索_outstanding表 62 public static string GetSql(string keyword, string sortName, string sortOrder) 63 { 64 // ""内要填写数据库中的表名 ,否则会出现 “Web 服务器被配置为不列出此目录的内容” 重要 65 string sql = @"SELECT * FROM outstanding"; 66 67 //搜索 68 if (keyword != "") 69 { 70 //先判断sortName是否多值模糊搜索 71 //这里目地是实现,用户想多条件搜索时,在搜索栏中用逗号隔开 72 //比如用户搜索同时有A和B的信息,那么在搜索栏中输入“A,B”或者“A,B” 73 var keywordList = keyword.Split(new char[2] { ',', ',' }); 74 75 //搜索条件 76 for (int i = 0; i < keywordList.Count(); i++) 77 { 78 sql += i == 0 ? " WHERE " : " AND "; 79 //()内要填写数据库表名中对应的字段名 ,否则会出现 “-” 单元格内取不到值,但表格能出来 重要 80 sql += @"CONCAT(id, type,Shipto,Soldto,MOsPONo,Item,Docrecodedate,Firstdate,SONo,SOItem,Material,SOQty,Vendor,PCPIC,DPT,Deliverydate,PONo,POItem,POqty,PackGRBal,POIssDate,Priorityorder,Lastreply,WHreservation,DRcode,Causeofdelay,Targetdate,TargetQTY,Column27,Column10,Remark,Column19,destination,supplier,Orderstatus,Inlibrarystate,ConfrimDel,Dateofdelivery,Supplierdeliverytime,Purchaseperiod,Ordercode,SalesinvoiceNo,明細No,ETDQTY,納入期日,date2,ShippingMethod,Deliverytimestorage,購買発注No,NO,Destination2,Buyer,QTY,Remarks,No3,item4,pgrp,ic,ac,grqty,loc,confirmedqty,reason,KEY1,KEY2) " 81 + "LIKE '%" + keywordList[i] + "%'"; 82 83 } 84 } 85 //排序 86 if (sortName != "") 87 { 88 //排序条件 89 sql += @" ORDER BY '" + sortName + "' " + sortOrder; 90 } 91 return sql; 92 } 93 94 //获取maillist数据库数据 95 public static string SearchSql(string keyword, string sortName, string sortOrder) 96 { 97 // ""内要填写数据库中的表名 ,否则会出现 “Web 服务器被配置为不列出此目录的内容” 重要 98 string sql = @"SELECT * FROM maillist"; 99 100 //搜索 101 if (keyword != "") 102 { 103 //先判断sortName是否多值模糊搜索 104 //这里目地是实现,用户想多条件搜索时,在搜索栏中用逗号隔开 105 //比如用户搜索同时有A和B的信息,那么在搜索栏中输入“A,B”或者“A,B” 106 var keywordList = keyword.Split(new char[2] { ',', ',' }); 107 108 //搜索条件 109 for (int i = 0; i < keywordList.Count(); i++) 110 { 111 sql += i == 0 ? " WHERE " : " AND "; 112 //()内要填写数据库表名中对应的字段名 ,否则会出现 “-” 单元格内取不到值,但表格能出来 重要 113 sql += @"CONCAT(id, type,Shipto,Soldto,MOsPONo,Item,Docrecodedate,Firstdate,SONo,SOItem,Material,SOQty,Vendor,PCPIC,DPT,Deliverydate,PONo,POItem,POqty,PackGRBal,POIssDate,Priorityorder,Lastreply,WHreservation,DRcode,Causeofdelay,Targetdate,TargetQTY,Column27,Column10,Remark,Column19,destination,supplier,Orderstatus,Inlibrarystate,ConfrimDel,Dateofdelivery,Supplierdeliverytime,Purchaseperiod,Ordercode,SalesinvoiceNo,明細No,ETDQTY,納入期日,date2,ShippingMethod,Deliverytimestorage,購買発注No,NO,Destination2,Buyer,QTY,Remarks,No3,item4,pgrp,ic,ac,grqty,loc,confirmedqty,reason,KEY1,KEY2) " 114 + "LIKE '%" + keywordList[i] + "%'"; 115 116 } 117 } 118 //排序 119 if (sortName != "") 120 { 121 //排序条件 122 sql += @" ORDER BY '" + sortName + "' " + sortOrder; 123 } 124 return sql; 125 } 126 }
Handler2.ashx文件
1 <%@ WebHandler Language="C#" Class="Handler2" %> 2 3 using System; 4 using System.Web; 5 using System.Data; 6 using System.Data.SqlClient; //数据库 7 using Newtonsoft.Json; //操作json库 8 9 public class Handler2 : IHttpHandler //变更数据库内容的操作,全部在Handler2中 10 { 11 //导入自定义标准库 12 CommonClass class1 = new CommonClass(); 13 14 public void ProcessRequest(HttpContext context) 15 { 16 context.Response.ContentType = "text/plain"; 17 18 //前端变更单元格内容写入数据库 OK 19 if (context.Request["RequestType"] == "save_data") //save_data是homePage.aspx中自定义的请求类型 20 { 21 int dbId = Convert.ToInt32(context.Request["DBID"]); //ID 22 string fieldName = context.Request["fieldName"].ToString(); //字段名 "FieldName"是homePage.aspx中传来的值 23 string modifyValue = context.Request["modifyValue"].ToString(); //值 "ModifyValue"是homePage.aspx中传来的值 24 25 //Console.Write(fieldName); 26 27 string s0 = "update outstanding set " + fieldName + "='" + modifyValue + "' where id=" + dbId; 28 context.Response.Write(s0); 29 30 //往数据库写入新信息 31 SqlConnection conn = new SqlConnection("server=*.*.*.*;database=***;uid=sscosd;pwd=****"); 32 conn.Open(); 33 SqlCommand cmd = new SqlCommand(s0, conn); //用来执行查询语句 34 SqlDataAdapter sda = new SqlDataAdapter(); //数据库适配器,用来充当数据库与数据集之间的桥梁 35 sda.SelectCommand = cmd; //选择命令向数据库发送(发送查询语句) 36 DataSet ds = new DataSet(); //创建一个数据集对象,相当于小型数据库,它当中存放若干个数据块 37 sda.Fill(ds, "cs"); //Fill: 填充 把数据填充名为“CS”的表中。 重要!!! 38 39 //确认查询语句,是否正确 40 context.Response.Write(s0); 41 }; 42 43 44 //前端删除行_写入数据库 45 if (context.Request["RequestType"] == "deleted_Row") //save_data是homePage.aspx中自定义的请求类型 46 { 47 string dbId = context.Request["DBID"].ToString(); //ID 48 string s0 = "delete from outstanding where id=" + dbId; 49 50 //往数据库写入新信息 51 SqlCommand cmd = new SqlCommand(s0, class1.GetConnection1()); //用来执行查询语句 52 SqlDataAdapter sda = new SqlDataAdapter(); //数据库适配器,用来充当数据库与数据集之间的桥梁 53 sda.SelectCommand = cmd; //选择命令向数据库发送(发送查询语句) 54 DataSet ds = new DataSet(); //创建一个数据集对象,相当于小型数据库,它当中存放若干个数据块 55 //sda.Fill(ds, "cs"); 56 57 //确认查询语句,是否正确 58 //context.Response.Write(s0); 59 }; 60 61 //下载excel文件 待理解 62 if (context.Request["RequestType"] == "download_file") 63 { 64 //string my_name = context.Request["my_name"].Trim(); 65 //string s1 = "select 公司代码 from 人员名单 where rtrim(ltrim(姓名))='" + my_name.Trim() + "'"; 66 //string pcode = class1.ExecScalar(class1.GetConnection1(), s1).Trim(); 67 //s1 = "select 识别码 as ID,vendor,po,item,iss_date,material,po_qty,del_date,gr_qty,os_qty,ship_to,loc,confirm_del,confirmed_qty,reason,记入者,回复时间 from 采购PO下载合并1"; 68 //s1 += " where left(rtrim(ltrim(vendor)),5)='"+pcode.Trim().Substring(0,5)+"'"; 69 //s1 += " order by vendor,material,po,item"; 70 //string filename = class1.this_DownloadToXlsxFromSql_new2A(class1.GetConnection1(),s1,4,"po_qty,gr_qty,os_qty,confirmed_qty"); 71 //context.Response.Write(filename); 72 string s1 = "select * from outstanding"; 73 string filename = class1.this_DownloadToXlsxFromSql_new2A(class1.GetConnection2(),s1,4,"po_qty,gr_qty,os_qty,confirmed_qty"); 74 context.Response.Write(filename); 75 } 76 77 //上载excel文件 78 if (context.Request["RequestType"] == "upload_file") 79 { 80 81 //string s1 = "update outstanding set Shipto='S777test' where id='5989' "; 82 //string filename = class1.this_DownloadToXlsxFromSql_new2A(class1.GetConnection2(),s1,4,"po_qty,gr_qty,os_qty,confirmed_qty"); 83 84 string s1 = "2023-05-05-092829.xlsx";//要上载的excel 85 //context.Response.Write(filename); 86 } 87 } 88 89 public bool IsReusable 90 { 91 get 92 { 93 return false; 94 } 95 } 96 97 }
浙公网安备 33010602011771号