我有以下代码输入 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 中使用逗号?