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;