vue+iview导入Excel数据,下载Excel模板,二

本文介绍如何使用Vue+IView组件库实现从Excel导入数据,下载Excel模板,并将数据提交到数据库。后端处理中,通过读取指定路径的Excel文件并转换为字符流,供前端下载。
摘要由CSDN通过智能技术生成

vue+iview实现导入Excel数据,下载Excel模板,提交数据到数据库

后端代码

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using SPC.DBModel.SYS;
using SPC.DBModel.BU;
using SPC.Models;
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Net;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using System.Net.Sockets;
using Newtonsoft.Json;
using System.Text;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.Data;
using System.Reflection;
using System.Data.SqlClient;
using System.Transactions;
using Microsoft.AspNetCore.Hosting;

namespace SPC.Controllers
{
   
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [Route("api/[controller]/[action]")]
    public class AnalysisSettingController : myBaseController
    {
   
        const string PriviageCode = "analysis-setting";
        private readonly IHttpContextAccessor httpContextAccessor;
        private readonly IHostingEnvironment _hostingEnvironment;

        public AnalysisSettingController(SPC_SYSDBContext _SYSDBContext, SPCDB_BUContext _BUContext, IConfiguration _configuration, ILogger<SampleController> _logger
            , IHostingEnvironment hostingEnvironment) : base(_SYSDBContext, _BUContext, _configuration, _logger)
        {
   
            _hostingEnvironment = hostingEnvironment;
        }

      
		//导入Excel
        public async Task<IActionResult> ParmasImport()
        {
   
            if (!Auth_ParmAndFun(PriviageCode, "")) return StatusCode(StatusCodes.Status403Forbidden);

            int bu =Convert.ToInt32( CurrentBUKey);
            List<getItemListResultClass> listResult = new List<getItemListResultClass>();
            var file = Request.Form.Files[0];
            try
            {
   
                string SaveToPath = Configuration.GetValue<string>("UploadSetting:SaveToPath");
                if (!Directory.Exists(SaveToPath))
                    return Ok(new {
    success = false, message = "未发现C:\temp资料夹" });
                //Directory.CreateDirectory(SaveToPath);

                string FilePath1 = SaveToPath + file.FileName;

                IWorkbook workbook = null;
                using (var ms = new MemoryStream())
                {
   
                    using (var stream = new FileStream(FilePath1, FileMode.Open))
                    {
   
                        await stream.CopyToAsync(ms);
                        ms.Position = 0;
                    }
                    
                    FileInfo fi = new FileInfo(FilePath1);
                    //fi.Delete();
                    string extension = System.IO.Path.GetExtension(FilePath1);

                    if (Path.GetExtension(file.FileName).ToLower() == ".xls")
                        workbook = new HSSFWorkbook(ms);
                    else
                        workbook = new XSSFWorkbook(ms);
                   
                    string Session_PARM_CATEGORY_ID = HttpContext.Session.GetString("PARM_CATEGORY_ID");
                    if (string.IsNullOrEmpty(Session_PARM_CATEGORY_ID))
                        Session_PARM_CATEGORY_ID = Guid.NewGuid().ToString();

                    int NumberOfSheets = workbook.NumberOfSheets;
                    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值