c# - 上传 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" });
}
解决方案
worksheet.Cell(1, 1).Value = "ID:";
worksheet.Cell(1, 2).Value = "name:";
worksheet.Cell(1, 3).Value = "surname:";
您还为 excel 使用了哪个库,例如我:使用 OfficeOpenXml;
推荐阅读
- node.js - 在环回中使模型的索引唯一
- asp.net - @Html.DisplayFor 导致 microsoft.aspnetcore.mvc.viewfeatures.internal.viewbuffer
- python - 如何使用 OpenCV-Python 从英特尔实感 D415 获取 IR1 和 IR2 摄像头图像?
- javascript - 使用 tablesorter 动态排序表
- webpack - Webpack splitChunks 插件 - 为什么设置块的优先级使其初始化异步?
- imagemagick - 我们如何使用php对png图像应用阈值效果
- angular - 有条件地在 ng2-smart-table 中添加自定义按钮
- java - JUnit - 在另一个类的测试中使用方法
- jquery - 我不知道如何使该框水平显示
- php - Yii2 Swiftmailer foreach 向第一个发送相同的电子邮件