Excel下载、Excel上传,邮件发送Excel并带Char图表

Excel上传

前段:ExportToExcel、UploadExcel

<!-- 模态框 -文件下载上传 --><div class="modal"><div class="modal-content family"><span class="close" onclick="ModalClose('modal')">×</span><p id="Item" class="modal-title">设置产出目标值</p><br><div class="form-flex"><label for="projcode">projcode:</label><select id="projcode" name="projcode" placeholder="select projcode"></select><label for="product">product:</label><select id="product" name="product" placeholder="select projcode"></select><label for="process">process:</label><select id="process" name="process" placeholder="select process"></select><label><span><label><button type="submit" style="background-color: #49c5ce;" onclick="ExportToExcel()"><span><img src="/images/ProcessCapacity/Excel.ico" alt="Excel" class="icon"></span>生成Excel模板</button></label></span><span id="export"></span></label><span><label>导入模板:</label>&nbsp;<span><input type="file" id="Template" name="Template" style="width:calc(100% - 0.78rem)" placeholder="Enter your Process"></span></span><label for="message">message:</label><input type="text" id="message" name="message" placeholder="Enter your message"><button type="submit" style="font-weight: bold; height:0.5rem;" onclick="UploadExcel()">Submit</button></div></div></div>

Excel下载

        /*目标值导出*/var ExportToExcel = function () {var projcode = $('#projcode').val();var product = $('#product').val();var pam = {type: ExportType,proj_code: $('#projcode').val(),product_code: $('#product').val() == null ? "ALL" : $('#product').val(),process_code: $('#process').val() == null ? "ALL" : $('#process').val()};//var url = "http://localhost:44304/Dashboard/ExportToExcel?" + $.param(pam);var url = fisApiUrl + "/Dashboard/ExportToExcel?" + $.param(pam);if (projcode && product) {var xhr = new XMLHttpRequest();xhr.open('GET',url,true);xhr.responseType = 'blob';xhr.onload = function () {if (xhr.status === 200) {var url = window.URL.createObjectURL(xhr.response);var a = document.createElement('a');a.href = url;a.download = ExportType.toLocaleUpperCase()+'.xlsx';a.click();window.URL.revokeObjectURL(url);}};xhr.send();}}

Excel上传

        /*目标值上传*/var UploadExcel = function () {var fileInput = document.getElementById("Template");var file = fileInput.files[0];var formData = new FormData();formData.append("file", file);//formData.append("type", uploadType);formData.append("type", ExportType);var projcode = $('#projcode').val();var product = $('#product').val();if (projcode && product) {$.ajax({type: "POST",//url: "http://localhost:44304/Dashboard/UploadExcel",url: fisApiUrl + "/Dashboard/UploadExcel",enctype: 'multipart/form-data',data: formData,async: false,processData: false,// 禁止将数据转换为查询字符串contentType: false,// 禁止设置请求头的Content-TypecrossDomain: true,success: function (data) {if (data.Success) {alert("successfully");//$('#product').empty();//$('#process').empty();$("#Template").val('');$("#message").val('');$(".modal").fadeOut();}},error: function () {console.log('Error No file uploaded..');}});}}

后端接口:Excel下载

        [HttpGet]public HttpResponseMessage ExportToExcel(string type,string proj_code,string product_code,string process_code){if (string.IsNullOrWhiteSpace(proj_code)){return new HttpResponseMessage(HttpStatusCode.InternalServerError); ;}DataTable dataTable = new DataTable();switch (type.Trim().ToUpper()){case "PRO_OUTPUT_TARGET"://工序产出目标值dataTable = new DashboardDAO().ExportToExcel(proj_code, product_code.Replace("null", null), process_code.Replace("null", null));break;case "EMP_OUTPUT_TARGET"://员工产出目标值dataTable = new DashboardDAO().OperatorTargetVauleToExcel(proj_code, product_code.Replace("null", null), process_code.Replace("null", null));break;case "EQUIPMENT_DATA":case "BINDING_EQUIPMENT"://工序&设备绑定dataTable = new DashboardDAO().BindingEquipmentToExcel(type.Trim().ToUpper(), proj_code, product_code.Replace("null", null), process_code.Replace("null", null));break;case "BINDING_TEST_ITEM"://工序&机台测试项绑定dataTable = new DashboardDAO().BindingTestItemToExcel(proj_code, product_code.Replace("null", null), process_code.Replace("null", null));break;}// 创建一个工作簿IWorkbook workbook = new XSSFWorkbook();// 创建一个工作表ISheet sheet = workbook.CreateSheet("Sheet1");// 创建表头行IRow headerRow = sheet.CreateRow(0);ICellStyle headerStyle = workbook.CreateCellStyle();headerStyle.FillForegroundColor = IndexedColors.Green.Index;headerStyle.FillPattern = FillPattern.SolidForeground;headerStyle.BorderTop = BorderStyle.Thin;headerStyle.BorderBottom = BorderStyle.Thin;headerStyle.BorderLeft = BorderStyle.Thin;headerStyle.BorderRight = BorderStyle.Thin;for (int i = 0; i < dataTable.Columns.Count; i++){//headerRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName);ICell cell = headerRow.CreateCell(i);cell.CellStyle = headerStyle;cell.SetCellValue(dataTable.Columns[i].ColumnName);}ICellStyle cellStyle = workbook.CreateCellStyle();cellStyle.BorderTop = BorderStyle.Thin;cellStyle.BorderBottom = BorderStyle.Thin;cellStyle.BorderLeft = BorderStyle.Thin;cellStyle.BorderRight = BorderStyle.Thin;// 填充数据行for (int i = 0; i < dataTable.Rows.Count; i++){IRow dataRow = sheet.CreateRow(i + 1);for (int j = 0; j < dataTable.Columns.Count; j++){//dataRow.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString());if (string.IsNullOrEmpty(dataTable.Rows[i][j].ToString())){ICell cell = dataRow.CreateCell(j);cell.CellStyle = cellStyle;cell.SetCellValue(dataTable.Rows[i][j].ToString());}else{ICell cell = dataRow.CreateCell(j);cell.CellStyle = headerStyle;cell.SetCellValue(dataTable.Rows[i][j].ToString());}}}// 将工作簿转换为字节数组using (MemoryStream stream = new MemoryStream()){workbook.Write(stream);byte[] excelBytes = stream.ToArray();// 创建一个 HttpResponseMessage 对象,并将 Excel 字节数组作为内容返回HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);response.Content = new ByteArrayContent(excelBytes);response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");response.Content.Headers.ContentDisposition.FileName = $@"{type.Trim().ToUpper()}.xlsx";response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");return response;}}

后端接口:Excel上传

        /// <summary>/// 上传模板/// </summary>/// <returns></returns>[HttpPost]public Result UploadExcel(){string type = string.Empty;var httpRequest = HttpContext.Current.Request;if (httpRequest.Files.Count == 0){return new Result(false) { ReturnMessage = "No file uploaded." };}var file = httpRequest.Files[0];if (file == null || file.ContentLength == 0){return new Result(false) { ReturnMessage = "No file uploaded." };}/*保存文件*///var fileName = Path.GetFileName(file.FileName);//var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/your-upload-directory"), fileName);//file.SaveAs(filePath);// 处理上传的 Excel 文件DataTable dataTable = new DataTable();using (var stream = file.InputStream){IWorkbook workbook = new XSSFWorkbook(stream);ISheet sheet = workbook.GetSheetAt(0);IRow headerRow = sheet.GetRow(0);for (int i = 0; i < headerRow.LastCellNum; i++){DataColumn column = new DataColumn(headerRow.GetCell(i).ToString());dataTable.Columns.Add(column);}for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++){IRow row = sheet.GetRow(i);if (row == null)continue;DataRow dataRow = dataTable.NewRow();for (int j = row.FirstCellNum; j < row.LastCellNum; j++){ICell cell = row.GetCell(j);if (cell != null){if (cell.CellType == CellType.String){dataRow[j] = cell.StringCellValue;}else if (cell.CellType == CellType.Numeric){dataRow[j] = cell.NumericCellValue.ToString();}}}dataTable.Rows.Add(dataRow);}}type = httpRequest.Form["type"].Trim();bool status = false;switch (type.Trim().ToUpper()){case "PRO_OUTPUT_TARGET"://工序产出目标值status = new DashboardDAO().UploadExcel(dataTable);break;case "EMP_OUTPUT_TARGET"://员工产出目标值status = new DashboardDAO().UploadOperatorTargetVauleExcel(dataTable);break;case "EQUIPMENT_DATA":case "BINDING_EQUIPMENT"://工序&设备绑定status = new DashboardDAO().UploadBindingEquipmentExcel(type.Trim().ToUpper(),dataTable);break;case "BINDING_TEST_ITEM"://工序&机台测试项绑定status = new DashboardDAO().UploadBindingTestItemExcel(dataTable);break;}//返回响应Result result = new Result(status);result.ReturnMessage = status ? "Excel file uploaded successfully." : "Excel file uploaded error!";return result;}

生成Excel带图表不做本地保存

        /// <summary>/// Excel图表不做本保存/// </summary>/// <param name="table"></param>[HttpPost]public void CreateExcelCharts3(List<string> emails){// 设置LicenseContext属性ExcelPackage.LicenseContext = LicenseContext.NonCommercial;ExcelPackage package = new ExcelPackage();// 创建一个工作表ExcelWorksheet sheet = package.Workbook.Worksheets.Add("Sheet1");// 创建行和单元格,并填充数据sheet.Cells["A1"].Value = "Name";sheet.Cells["B1"].Value = "Age";sheet.Cells["A2"].Value = "John";sheet.Cells["B2"].Value = 25;sheet.Cells["A3"].Value = "Jane";sheet.Cells["B3"].Value = 30;// 创建柱状图var chart = sheet.Drawings.AddChart("Chart1", OfficeOpenXml.Drawing.Chart.eChartType.ColumnClustered);// 设置图表位置chart.SetPosition(4, 0, 5, 0);// 设置图表大小chart.SetSize(400, 300); // 添加图表数据var series = chart.Series.Add(sheet.Cells["B2:B3"], sheet.Cells["A2:A3"]);// 配置图表样式chart.Title.Text = "当天工序产出";chart.Title.Font.Bold = true;// 将图表右移一个单元格chart.SetPosition(4, 4, 5, 4);// 保存Excel文件1//package.SaveAs(new FileInfo("output.xlsx"));// 保存Excel文件2//string filePath = @"D:\项目开发Develop\your_file_name.xlsx";//File.WriteAllBytes(filePath, package.GetAsByteArray());// 将Excel文件保存到内存流using (MemoryStream stream = new MemoryStream()){package.SaveAs(stream);stream.Position = 0;List<StreamAttachment> attachment = new List<StreamAttachment>(){new StreamAttachment{stream = stream,name = "当天工序产出.xlsx",mediaTyp = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}};EmailHelper.SendToEmail(emails, "看板指标预警", "Hi All: \n    当天工序产出现异常,详细数据,请查看附件!谢谢!", true, attachment, "Dashboard");}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/140579.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

分类预测 | Matlab实现PSO-LSTM粒子群算法优化长短期记忆神经网络的数据多输入分类预测

分类预测 | Matlab实现PSO-LSTM粒子群算法优化长短期记忆神经网络的数据多输入分类预测 目录 分类预测 | Matlab实现PSO-LSTM粒子群算法优化长短期记忆神经网络的数据多输入分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Matlab实现PSO-LSTM粒子群算法优化长短…

下载并安装DevEco Studio 3.1,初尝鸿蒙编程

摘自华为官网 DevEco Studio 3.1配套支持HarmonyOS 3.1版本及以上的应用及服务开发&#xff0c;提供了代码智能编辑、低代码开发、双向预览等功能&#xff0c;以及轻量构建工具DevEco Hvigor 、本地模拟器&#xff0c;持续提升应用及服务开发效率。 下载 官网下载地址 HUAWEI…

用python将csv表格数据做成热力图

python的开发者为处理表格和画图提供了库的支持&#xff0c;使用pandas库可以轻松完成对csv文件的读写操作&#xff0c;使用matplotlib库提供了画热力图的各种方法。实现这个功能首先需要读出csv数&#xff0c;然后设置自定义色条的各种属性如颜色&#xff0c;位置&#xff0c;…

19、Flink 的Table API 和 SQL 中的自定义函数及示例(3)

Flink 系列文章 1、Flink 部署、概念介绍、source、transformation、sink使用示例、四大基石介绍和示例等系列综合文章链接 13、Flink 的table api与sql的基本概念、通用api介绍及入门示例 14、Flink 的table api与sql之数据类型: 内置数据类型以及它们的属性 15、Flink 的ta…

IDEA常用快捷键大全(详解)

如何在IDEA中进行内容全局查找 在idea中进行全局查找&#xff0c;可以使用快捷键“Ctrl Shift F”或者在菜单栏中选择Edit > Find > Find in Path。在弹出的界面中&#xff0c;输入要查找的内容。如果“Ctrl Shift F”这个快捷键无法实现全局查找&#xff0c;可以尝…

CoRL 2023 获奖论文公布,manipulation、强化学习等主题成热门

今年大模型及具身智能领域有了非常多的突破性进展&#xff0c;作为机器人学与机器学习交叉领域的全球顶级学术会议之一&#xff0c;CoRL也得到了更多的关注。 CoRL 是面向机器人学习的顶会&#xff0c;涵盖机器人学、机器学习和控制等多个主题&#xff0c;包括理论与应用。今年…

Python与ArcGIS系列(四)在地图文档中加入图层

目录 0 简述1 将图层添加到地图文档中2 将图层插入到地图文档0 简述 本篇介绍如何利用arcpy实现将图层添加到地图文档中,以及将图层插入到地图文档指定的位置。 1 将图层添加到地图文档中 arcpy的mapping模块提供的AddLayer()函数可以实现将图层添加到地图文档中。功能本质上…

初探地理编码(2023.11.12)

地理编码相识 2023.11.12 引言1、地理编码简介2、地理编码API和服务&#xff08;解决方案供应商 / 厂商&#xff09;2.1 高德2.2 百度2.3 超图2.4 天地图2.5 ArcGIS2.6 MapBox2.7 Cesium2.8 MapLocation 3、python实例3.1 pip安装依赖库&#xff08;python 3.6&#xff09;3.2 …

MVVM框架:图片加载有问题

一、前言&#xff1a;在我使用ImageView加载图片的时候添加如下代码发现报错 app:imageUrl"{viewModel.observableField.assetImg}"报错如下错误 二、原因&#xff1a;是啥我不太清楚好像是没有imageView的适配器&#xff0c;后来我看了一下确实没有 public class I…

Leetcode300 最长递增子序列

给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。 示例 1&#xf…

AD9371 Crossbar 和 I、Q数据 映射JESD204B传输层

AD9371 系列快速入口 AD9371ZCU102 移植到 ZCU106 &#xff1a; AD9371 官方例程构建及单音信号收发 ad9371_tx_jesd -->util_ad9371_xcvr接口映射&#xff1a; AD9371 官方例程之 tx_jesd 与 xcvr接口映射 AD9371 官方例程 时钟间的关系与生成 &#xff1a; AD9371 官方…

okhttp添加公共参数

在项目开发中很多时候后台都会给一些全局的公共入参&#xff0c;比如携带手机信息或者时间戳等字段。而我们在使用okhttp时&#xff0c;就需要我们单独就行二次封装处理了&#xff0c;对于请求全局参数&#xff0c;每次请求都要去写一次&#xff0c;那是肯定不行的。 所以就要我…

linux 安装 mini conda,linux下安装 Miniconda

下载地址 https://docs.conda.io/projects/miniconda/en/latest/index.html 安装conda mkdir -p ~/miniconda3 wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh bash ~/miniconda3/miniconda.sh -b -u -p ~/mini…

3. 深度学习——损失函数

机器学习面试题汇总与解析——损失函数 本章讲解知识点 什么是损失函数?为什么要使用损失函数?详细讲解损失函数本专栏适合于Python已经入门的学生或人士,有一定的编程基础。 本专栏适合于算法工程师、机器学习、图像处理求职的学生或人士。 本专栏针对面试题答案进行了优化…

AR打卡小程序:构建智能办公的新可能

【内容摘要】 随着技术的飞速发展&#xff0c;智能办公已不再是遥不可及的梦想。在这其中&#xff0c;AR打卡小程序以其独特的技术优势&#xff0c;正逐步成为新型办公生态的重要组成部分。本文将探讨AR打卡小程序的设计理念、技术实现以及未来的应用前景&#xff0c;并尝试深…

微信开发者工具如何使用

首先是下载微信开发者工具 链接: https://pan.baidu.com/s/1ri-fRCUQsz9vLxbTqtdPUQ 提取码: 8rhn 复制这段内容后打开百度网盘手机App&#xff0c;操作更方便哦 安装完打开就是以下界面 接下来进入正题 第一步新建或导入 新建 获取AppID 第一步&#xff1a;通过微信公众平…

第12章 PyTorch图像分割代码框架-3:推理与部署

推理模块 模型训练完成后&#xff0c;需要单独再写一个推理模块来供用户测试或者使用&#xff0c;该模块可以命名为test.py或者inference.py&#xff0c;导入训练好的模型文件和待测试的图像&#xff0c;输出该图像的分割结果。inference.py主体部分如代码11-7所示。 代码11-7 …

C++实例 调用Tesseract OCR的API

C实例 调用Tesseract OCR的API 1. 前言2. 模式3. 调用方式C Examples**【转自官网】3.1 Basic_example3.2 SetRectangle_example3.3 GetComponentImages_example3.4 ResultIterator_example3.5 OSD_example3.6 LSTM_Choices_example3.7 OpenCV_example3.8 UserPatterns_example…

vue做的一个一点就转的转盘(音乐磁盘),点击停止时会在几秒内缓慢停止,再次点击按钮可以再次旋转,

先看效果&#xff1a; 代码&#xff1a;主要部分我会红线画出来 css:部分&#xff1a; 源码&#xff1a; vue部分&#xff1a; <template><div class"song-lyric"><div><div class"type"><div class"right">&l…

如何用自然语言 5 分钟构建个人知识库应用?我的 GPTs builder 尝试

开发者的想象力闸门一旦打开&#xff0c;迎接我们的必然是目不暇接的 AI 应用浪潮冲击。 兴奋 早晨&#xff0c;我突然发现 ChatGPT 最新的 Create GPTs 功能可以用了。 这太让我意外了&#xff0c;没想到这么快。根据页面上的提示&#xff0c;我一直以为还得等上一周左右。于是…