问题标签 [csvhelper]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
458 浏览

c# - 来自 csv 导入方法的“不要多次处理对象”警告

我阅读了很多关于这个的帖子,但仍然需要一些帮助来理解这一点。

CodeAnalysis 是说这个方法是两次处理对象。实际上,它警告我方法中的两个对象;一次用于对象file.InputStream,一次用于reader对象

这是我的代码:

谢谢

0 投票
3 回答
586 浏览

c# - Fail try/catch block reading a csv

I'm unit testing my C# application that Parses a CSV. I'm at 94% code coverage, because I can't force it to fail the try/catch blocks... I'm using CsvHelper from Nuget http://joshclose.github.io/CsvHelper

I've tried passing the most obscure strings I could think of to it, but it makes it through this, and errors out later on in the function..

After the try/catch blocks, I have a section that enforces all the headers exist, and it fails there, but it should be failing during the reading, for this example. How do I force my unit test to fail? Any help is appreciated! Thanks in advance.

0 投票
4 回答
6881 浏览

c# - CSVHelper 必填字段

解析 csv 文件时,我如何定义特定字段是强制性的。本质上,我想确保给定的字段永远不会为空,如果是,那么我希望抛出异常。这是映射类:

和用法:

目前我只是检查数据列表中的结果如下:

我无法在 CSVHelper 文档中找到内置功能。我错过了什么吗?

0 投票
1 回答
4267 浏览

c# - 获取控制台的 TextWriter

我正在使用CsvHelper,它使用 aTextWriter来写入其输出。我不想将内容写入文件,而是将内容写入控制台。如何获得TextWriter将写入控制台的内容?

0 投票
2 回答
11397 浏览

c# - CsvHelper ignore not working

I am using CsvHelper to generate a csv file based on a List, but I would like to avoid writing one of the values. As per the documentation, I used a CsvClassMap to specify the field that I want to ignore. However, the value is still being written to the file.

Here is my class:

Here is my CsvClassMap:

And this is the code that I am using to generate the output:

My output is as follows:

How can I get it to stop writing the MiddleName?

0 投票
3 回答
19662 浏览

c# - 使用 CsvHelper 解析标题包含空格的 CSV

我有一个带有字段标题的 CSV 文件,其中一些包含两个或三个用空格分隔的单词:

如上所述,电子表格应用程序中电子表格前几行的屏幕截图,包括包含空格的标题。

您可以在上图中看到包含空格的字段标题:
“时间”、“进程名称”和“图像路径”。

当我尝试通过调用reader.GetRecords<DataRecord>();DataRecord我定义的类在哪里)来读取 CSV 时,出现错误:

CSV 文件中不存在字段“TimeOfDay”。"*

这是因为我的DataRecord班级不能包含带空格的成员。

如何使用 CsvHelper 解析 CSV 文件?

0 投票
1 回答
5402 浏览

c# - 使用 CsvHelper、CsvReader.Read() 时如何忽略空格?

当使用 CsvHelper 库,特别是CsvReader.Read()函数时,有没有办法忽略空白记录和/或空格?

我需要返回原始string[]文件,但希望在解析库时可以执行一些清理功能。

我检查了 Github 并且CsvReader.Read()似乎使用了 SkipEmptyRecords但这对我不起作用,因为我有空格。

这是我的 csv 文件,它以 UTF8 编码,没有 BOM。

在此处输入图像描述

我已经尝试过 ASCII 编码,以防我遗漏了一些东西,但这也不起作用。
如果没人知道,我会和 Josh 聊天并提交一个带有修复的 git 请求。

参考:http: //joshclose.github.io/CsvHelper/

0 投票
0 回答
118 浏览

c# - CsvHelper 可以创建“派生”行,例如小计吗?

我最近使用 CsvHelper 简化了许多报告导出取得了很多成功,但我目前对如何输出“小计”行感到困惑。

例如,如果我要导出的强类型对象集合是员工每天工作的小时数,我想在单独的行中输出该员工所有小时数的小计。

CsvHelper 甚至可能吗?如果是,我怀疑它使用的是自定义 TypeConverter,但如果是这种情况,我不确定如何创建一个不“基于”我的强类型对象的新行。

0 投票
1 回答
1131 浏览

c# - CsvHelper 如何在同一个文件中保存多个类

如何使用 CsvHelper 在同一个文件中保存多个类?例如,通过不同的工作表。我试过这样,但不幸的是它不起作用。

0 投票
1 回答
2107 浏览

vb.net - .net 创建对象类型来自字符串的对象列表

我需要一种创建对象列表的方法,其中对象来自字符串

例如,我希望将以下内容转换为对象列表

最终结果

感谢 xxbbcc 为我指明了正确的方向。我想出了下面的解决方案,它可以让我上传任何 csv 文件并将其解析为 x 对象列表。

这是通过 dropzone 上传文件以及表名、对象类名和映射类名来实现的。这现在允许我使用准备导入临时表的 csvhelper 解析文件的内容。