private void CreateNewCSVFile(List<ProductWrapper> products, string saveFilePath, string excelFileMapperPath)
{
try
{
/*
* The license file is an embedded resource in the NopCommon.dll
*/
License license = new License();
license.SetLicense("Aspose.Cells.lic"); // Aspose_Cells
Workbook workbook = new Workbook(excelFileMapperPath);
// Open the 1st Worksheet and go to work
Worksheet worksheet = workbook.Worksheets[0];
int rowIndex = 1;
foreach (var product in products)
{
worksheet.Cells[rowIndex, 2].PutValue(product.FirstName);
worksheet.Cells[rowIndex, 3].PutValue(product.LastName);
rowIndex++;
}
// to create a physical file for testing
workbook.Save(saveFilePath, SaveFormat.CSV);
workbook = null;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
*** If this post helps you, then Mark this post as Answer ***
// CSV rules: http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules
// From the rules:
// 1. if the data has quote, escape the quote in the data
// 2. if the data contains the delimiter (in our case ','), double-quote it
// 3. if the data contains the new-line, double-quote it.
if (data.Contains("\""))
{
data = data.Replace("\"", "\"\"");
}
if (data.Contains(","))
{
data = String.Format("\"{0}\"", data);
}
if (data.Contains(System.Environment.NewLine))
{
data = String.Format("\"{0}\"", data);
}
*** If this post helps you, then Mark this post as Answer ***
GrassProgram...
Member
369 Points
257 Posts
Proper format for a CSV file
Jul 18, 2012 02:33 PM|LINK
What is the proper format for a CSV file? Some of the fields I need have commas in them, which makes my life difficult.
Specs
Member
618 Points
152 Posts
Re: Proper format for a CSV file
Jul 18, 2012 02:44 PM|LINK
Not sure if you are willing to purchase this, but Aspose makes dealing with excel files/csv rediculously easy.
http://www.aspose.com/
ex:
private void CreateNewCSVFile(List<ProductWrapper> products, string saveFilePath, string excelFileMapperPath) { try { /* * The license file is an embedded resource in the NopCommon.dll */ License license = new License(); license.SetLicense("Aspose.Cells.lic"); // Aspose_Cells Workbook workbook = new Workbook(excelFileMapperPath); // Open the 1st Worksheet and go to work Worksheet worksheet = workbook.Worksheets[0]; int rowIndex = 1; foreach (var product in products) { worksheet.Cells[rowIndex, 2].PutValue(product.FirstName); worksheet.Cells[rowIndex, 3].PutValue(product.LastName); rowIndex++; } // to create a physical file for testing workbook.Save(saveFilePath, SaveFormat.CSV); workbook = null; } catch (Exception ex) { throw new Exception(ex.Message); } }Specs
Member
618 Points
152 Posts
Re: Proper format for a CSV file
Jul 18, 2012 02:46 PM|LINK
Or you can try this:
// CSV rules: http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules // From the rules: // 1. if the data has quote, escape the quote in the data // 2. if the data contains the delimiter (in our case ','), double-quote it // 3. if the data contains the new-line, double-quote it. if (data.Contains("\"")) { data = data.Replace("\"", "\"\""); } if (data.Contains(",")) { data = String.Format("\"{0}\"", data); } if (data.Contains(System.Environment.NewLine)) { data = String.Format("\"{0}\"", data); }niceastham
Member
645 Points
175 Posts
Re: Proper format for a CSV file
Jul 18, 2012 03:25 PM|LINK
Could you use a different delimiter....
for example a pipe |
ryanbesko
Contributor
3607 Points
630 Posts
Re: Proper format for a CSV file
Jul 18, 2012 04:05 PM|LINK
You can put quotes around the fields that are text (called a text qualifier) so your format would be like this:
"Company Name, Inc.","1234 Some St","East Lansing","MI",48823,01/01/1999
Ramesh Chand...
Star
12922 Points
2672 Posts
Re: Proper format for a CSV file
Jul 18, 2012 06:55 PM|LINK
ryanbesko has provided a good solution, just you have to check that there should not be a quotes in your fields