You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core Razor Pages:如何上传CSV文件并转为数据库对象?

ASP.NET Core Razor Pages CSV导入转对象实现思路

1. 定义数据模型

先创建与CSV字段对应的Customer实体类,明确属性类型,方便后续映射:

public class Customer
{
    public string Name { get; set; }
    public string Address { get; set; }
    public DateTime DateOfBirth { get; set; }
}

2. 构建上传页面

在Razor Page(比如Index.cshtml)中添加文件上传表单,限制仅接受CSV文件,同时预留错误提示和解析结果展示区域:

@page
@model IndexModel

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label>选择CSV文件</label>
        <input type="file" name="csvFile" accept=".csv" class="form-control" required />
        <small class="form-text text-muted">CSV需包含表头:姓名、地址、出生日期</small>
    </div>
    <button type="submit" class="btn btn-primary mt-2">上传解析</button>

    @if (!string.IsNullOrEmpty(ViewData["Error"] as string))
    {
        <div class="alert alert-danger mt-3">@ViewData["Error"]</div>
    }

    @if (Model.Customers?.Any() == true)
    {
        <h3 class="mt-4">解析结果</h3>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>地址</th>
                    <th>出生日期</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var customer in Model.Customers)
                {
                    <tr>
                        <td>@customer.Name</td>
                        <td>@customer.Address</td>
                        <td>@customer.DateOfBirth.ToString("yyyy-MM-dd")</td>
                    </tr>
                }
            </tbody>
        </table>
    }
</form>

3. 文件上传与CSV解析逻辑

在对应的PageModel(Index.cshtml.cs)中处理上传请求,使用CsvHelper(NuGet安装)简化解析流程,同时做基础校验和错误处理:

using CsvHelper;
using CsvHelper.Configuration;
using System.Globalization;
using System.Text.Json;

public class IndexModel : PageModel
{
    public List<Customer> Customers { get; set; }

    public async Task<IActionResult> OnPostAsync(IFormFile csvFile)
    {
        // 基础校验
        if (csvFile == null || csvFile.Length == 0)
        {
            ViewData["Error"] = "请选择有效的CSV文件";
            return Page();
        }
        if (!csvFile.FileName.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
        {
            ViewData["Error"] = "仅支持CSV格式文件";
            return Page();
        }

        try
        {
            // 配置CSV解析规则
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                HasHeaderRecord = true,
                TrimOptions = TrimOptions.Trim // 自动去除字段前后空格
            };

            // 读取文件并映射到Customer对象
            using var reader = new StreamReader(csvFile.OpenReadStream());
            using var csv = new CsvReader(reader, config);
            csv.Context.RegisterClassMap<CustomerMap>();
            Customers = csv.GetRecords<Customer>().ToList();

            // 暂存数据(这里用Session示例,也可存本地JSON/内存缓存)
            HttpContext.Session.SetString("ParsedCustomers", JsonSerializer.Serialize(Customers));
        }
        catch (Exception ex)
        {
            ViewData["Error"] = $"解析失败:{ex.Message}";
            return Page();
        }

        return Page();
    }

    // 自定义表头-属性映射规则
    private class CustomerMap : ClassMap<Customer>
    {
        public CustomerMap()
        {
            Map(m => m.Name).Name("姓名");
            Map(m => m.Address).Name("地址");
            Map(m => m.DateOfBirth).Name("出生日期").TypeConverterOption.Format("yyyy-MM-dd");
        }
    }
}

4. 数据暂存方案(避免直接存SQL)

根据需求选择合适的暂存方式:

  • Session存储:适合临时保存用户上传的单次数据,需在Program.cs中配置Session:
    builder.Services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromMinutes(30);
        options.Cookie.IsEssential = true;
    });
    
    // 中间件中启用Session
    app.UseSession();
    
  • 本地JSON文件:适合需要持久化临时数据的场景,示例代码:
    var tempPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "temp");
    Directory.CreateDirectory(tempPath);
    var jsonFilePath = Path.Combine(tempPath, $"{Guid.NewGuid()}_customers.json");
    await System.IO.File.WriteAllTextAsync(jsonFilePath, JsonSerializer.Serialize(Customers));
    
  • 内存缓存:适合多用户共享临时数据(需注意并发和内存占用),使用IMemoryCache注入实现。

5. 额外注意事项

  • 安装CsvHelper:通过NuGet包管理器搜索安装,或执行命令 Install-Package CsvHelper
  • 编码处理:如果CSV包含中文,可在StreamReader中指定编码(如new StreamReader(..., Encoding.GetEncoding("GB2312")))
  • 大文件优化:若CSV文件过大,改用csv.Read()逐行解析,避免一次性加载全部数据到内存
  • 数据校验:可在映射后添加自定义校验逻辑(如检查姓名非空、出生日期有效),将错误信息返回给用户

内容的提问来源于stack exchange,提问作者Rockets23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 19:40:56