首页 > 解决方案 > 上传 excel 文件并在 aspnet core 中添加手动值

问题描述

嗨,我是 aspnet 核心的新手,我有将 excel 文件上传到数据库的功能并且已经在工作,我想在“状态”列中添加手动值,值为“otw”。这个值不是来自 excel 文件,所以我想在源代码中添加手册我该怎么做

我的表字段。etd,eta,container,reff,landing_site,sap_code,cases,qty_pl,usd_price,ex_rate,status.

public IActionResult Import(IFormFile postedFile)
{
    if (postedFile != null)
    {
        //Create a Folder.
        string path = Path.Combine(this.Environment.WebRootPath, "Uploads");
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        //Save the uploaded Excel file.
        string fileName = Path.GetFileName(postedFile.FileName);
        string filePath = Path.Combine(path, fileName);
        using (FileStream stream = new FileStream(filePath, FileMode.Create))
        {
            postedFile.CopyTo(stream);
        }

        //Read the connection string for the Excel file.
        string conString = this.Configuration.GetConnectionString("ExcelConString");
        DataTable dt = new DataTable();
        conString = string.Format(conString, filePath);

        using (OleDbConnection connExcel = new OleDbConnection(conString))
        {
            using (OleDbCommand cmdExcel = new OleDbCommand())
            {
                using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
                {
                    cmdExcel.Connection = connExcel;

                    //Get the name of First Sheet.
                    connExcel.Open();
                    DataTable dtExcelSchema;
                    dtExcelSchema = connExcel
                        .GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                    connExcel.Close();

                    //Read Data from First Sheet.
                    connExcel.Open();
                    cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
                    odaExcel.SelectCommand = cmdExcel;
                    odaExcel.Fill(dt);
                    connExcel.Close();
                }
            }
        }

        //Insert the Data read from the Excel file to Database Table.
        conString = this.Configuration.GetConnectionString("DefaultConnection");
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            {
                sqlBulkCopy.DestinationTableName = "dbo.Rm";

                sqlBulkCopy.ColumnMappings.Add("etd", "etd");
                sqlBulkCopy.ColumnMappings.Add("eta", "eta");
                sqlBulkCopy.ColumnMappings.Add("container", "container");
                sqlBulkCopy.ColumnMappings.Add("reff", "reff");
                sqlBulkCopy.ColumnMappings.Add("landing_site", "landing_site");
                sqlBulkCopy.ColumnMappings.Add("sap_code", "sap_code");
                sqlBulkCopy.ColumnMappings.Add("cases", "cases");
                sqlBulkCopy.ColumnMappings.Add("qty_pl", "qty_pl");
                sqlBulkCopy.ColumnMappings.Add("usd_price", "usd_price");
                sqlBulkCopy.ColumnMappings.Add("ex_rate", "ex_rate");
                // in this fiedl i want add manual 
                sqlBulkCopy.ColumnMappings.Add("status", );
                con.Open();
                sqlBulkCopy.WriteToServer(dt);
                con.Close();
            }
        }
    }
    return RedirectToAction("List", new { status = "otw" });
}

标签: c#entity-frameworkasp.net-core

解决方案


 worksheet.Cell(1, 1).Value = "ID:";
 worksheet.Cell(1, 2).Value = "name:";
 worksheet.Cell(1, 3).Value = "surname:";

您还为 excel 使用了哪个库,例如我:使用 OfficeOpenXml;


推荐阅读