我輸入一個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+", "[email protected]"""" + ", " + item.Photo1 + ", " + item.Photo1 +
", " + item.Photo1);
}
}
}
編輯:我剛剛意識到,第二行正在從輸入文件的讀取自動加入。我只需要找到一種方法來忽略第一行。這些領域似乎也不再被抵消。現在只是如何在csv中使用逗號?
我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –