我有以下代码输入CSV文件,然后将其转换并输出CSV文件。我正在使用FileHelper进行CSV读取。我遇到了一些问题。首先,插入一个带有字段信息的额外行(见下文)。第二个问题是我添加了双引号,所以我可以在包含逗号的单元格中有一个字符串;我找不到格式化的任何问题,但它不会导入到Excel中,只显示双引号和第一项。我在导入到Excel时尝试使用双引号来转义文本选项。第三个问题是某些字段是空白的,导致后来的字段偏移。
第二行字段:
, base, 0, 0, 0, Product Name, Size, Brand, Gender, Type, SKU, Stock, Sí, "Photo1, Photo2, Photo3", Photo1, Photo1, Photo1
代码:
using System;
using FileHelpers;
using System.IO;
[DelimitedRecord(";")]
public sealed class StoreItems
{
[FieldOptional()]
public String Attribut;
[FieldOptional()]
public String ProductName;
[FieldOptional()]
public String Size;
[FieldOptional()]
public String Brand;
[FieldOptional()]
public String Type;
[FieldOptional()]
public String ParentSKU;
[FieldOptional()]
public String SKU;
[FieldOptional()]
public String Stock;
[FieldOptional()]
public String RetailEURO;
[FieldOptional()]
public String SuggestedsellEURO;
[FieldOptional()]
public String Weight;
[FieldOptional()]
public String Description;
[FieldOptional()]
public String Photo1;
[FieldOptional()]
public String Photo2;
[FieldOptional()]
public String Photo3;
[FieldOptional()]
public String Gender;
[FieldOptional()]
public String EAN13;
}
public class RunProgram
{
public static void Main()
{
FileHelperEngine engine = new FileHelperEngine(typeof(StoreItems));
StoreItems[] res = engine.ReadFile("Presta.csv") as StoreItems[];
Directory.CreateDirectory("Output");
StreamWriter sw = new StreamWriter(@".\Output\" + DateTime.Now.ToString("hhmmss") + ".csv");
sw.AutoFlush = true;
Console.SetOut(sw);
Console.WriteLine("_type, _product_websites, tax_class_id, _visibility, is_in_stock, name, taglia, manufacturer, gender, _attribute_set, sku, qty, use_external_images, external_gallery, " +
"thumbnail_external_url, small_image_external_url, image_external_url");
foreach(StoreItems item in res)
{
string newtype = null;
int visibility = 0;
if (item.Attribut == "Parent")
{
newtype = "Configurable";
visibility = 4;
}
else if (item.Attribut == "Child")
{
newtype = "Simple";
visibility = 1;
}
int isinstock;
int stockint;
try
{
stockint = Convert.ToInt32(item.Stock);
}
catch
{
stockint = 0;
}
if (stockint > 0)
{
isinstock = 1;
}
else
{
isinstock = 0;
}
Console.WriteLine(newtype + ", base, 0, " + visibility + ", " + isinstock + ", " + item.ProductName + ", " + item.Size + ", " +
item.Brand + ", " + item.Gender + ", " + item.Type + ", " + item.SKU + ", " + item.Stock + ", Sí, " + @""""+item.Photo1+ ", "+item.Photo2+", "+item.Photo3+@"""" + ", " + item.Photo1 + ", " + item.Photo1 +
", " + item.Photo1);
}
}
}
编辑:我刚刚意识到第二行是从输入文件的读取中添加的。我只需要找到一种方法来忽略第一行。这些领域似乎也不再是抵消了。现在如何在csv中使用逗号?
答案 0 :(得分:0)
一个问题;你有;
string newtype = null;
int visibility = 0;
if (item.Attribut == "Parent")
{
newtype = "Configurable";
visibility = 4;
}
else if (item.Attribut == "Child")
{
newtype = "Simple";
visibility = 1;
}
如果item.Attribut
完全不是“父”或“儿童”,则不会设置newtype
。因此,为什么示例输出中的第一个字段为空。
答案 1 :(得分:0)
第二个问题;
您不必使用逗号作为分隔符,您可以使用任何所需的字符。因此,作为快速修复,为什么不将输出中的','分隔符更改为“|”。
当然,如果您还需要输出'|'在你的文字中,这不会有帮助,但如果没有 - 快速解决方法让你前进。