Problem in Setting column width of a csv file Using C#

Hi,


I have write a code for importing datatable in to a work book. I have set the column width of the worksheet but it is not working.

var workBook = new Workbook();
workBook.FileFormat = FileFormatType.CSV;
Worksheet worksheet = workBook.Worksheets[0];
worksheet.Cells.ImportDataTable(devDataTable, true, “A1”);

===========================================
worksheet.Cells.StandardWidth = 20.5;
===========================================
worksheet.Cells.SetColumnWidth(1,20.5);
==============================================

I have tried these two techniques but both are not working.


Hi,


Well, it should work fine. Could you create a sample console application (runnable), zip it and post it here to reproduce the issue on our end. We will check your issue soon. For datatable, I think you may create dynamic datatable in your code then import the table into the worksheet cells to show the issue on our end.

And, please try to use our latest version/fix, i.e…, Aspose.Cells for .NET v7.5.1

Thank you.

Hi,


This is my method

private void ExportDataTableToCSVFile(DataTable devDataTable)
{
var workBook = new Workbook();
workBook.FileFormat = FileFormatType.CSV;
Worksheet worksheet = workBook.Worksheets[0];
worksheet.Cells.ImportDataTable(devDataTable, true, “A1”);
worksheet.Cells.StandardWidth = 20.5;
workBook.Save(this.exportFileName);
}


Hi,


I need to know how do you create your DataTable i.e. “devDataTable” etc., so I requested you to create a sample console application to evaluate your issue properly on our end.

Thank you.

Hi,


I have created the datatable as

var devDataTable = new DataTable();

I donot know whether it will happens when i will create a sample console app

Hi,


"
var devDataTable = new DataTable();"
Well, the line of you pasted will only create a blank datatable, I needed to know how you have created columns in it and added data/rows to the columns etc.

See the sample code for your reference:

Sample code:

Workbook workbook = new Workbook();
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

DataSet ds = new DataSet();

System.Data.DataTable dt = new DataTable(“Table1”);
dt.Columns.Add(“Customer Identification Number”, typeof(string));
dt.Columns.Add(“Customer Name”, typeof(string));
dt.Columns.Add(“Description Text”, typeof(string));
for (int i = 1; i <= 200; i++)
{
System.Data.DataRow dr = dt.NewRow();
dr[“Customer Identification Number”] = i.ToString();
dr[“Customer Name”] = "Name " + i;
dr[“Description Text”] = "Description " + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);

worksheet.Cells.ImportDataTable(ds.Tables[0], true, 0, 0, false);

workbook.Save(“e:\test\2out1.xlsx”);

Hi,


I have tried this way also but that problem is not solved.

also i have set this as
worksheet.AutoFitColumns();
still i have that problem.

I have to mention one more thing that in the end of the file contains the following message…

This file is created with Aspose.Cells for .NET for evaluation only with an Evaluation License.
It is strictly prohibited from using it in the production of any software.
Any violation of this provision shall require a mandatory purchase of pay license
as well as expose the user to other legal recourse for collection and punitive damages.
Copyright 2003 - 2013 Aspose Pty Ltd.

Is the problem is occurs because of this message?

Hi,


You are getting this message/description test because you are not using a valid license with the product, you got to use a valid license and set it in your code to avoid this message or any other limitation (see the document for your reference: http://www.aspose.com/docs/display/cellsnet/Licensing). Also, your issue is not due to the message or due to the fact that you are not using the license with the product in your project/ application, it might be due to other things.
As we requested earlier, unless we have your runnable sample console application, we cannot evaluate your issue properly. Please create a sample console application (runnable), zip it and post it here to reproduce the issue on our end. We will check your issue soon.

And, use our latest version/fix i.e. Aspose.Cells for .NET v7.5.1.x

Thank you.

Hi,


I have attached the Exe of my demo application.

Hi,


I am unable to attach. How can I attach my ZIp file.

Hi,


Please provide us the complete sample source project/ application instead of a single Exe (portable executable). I want to see your source code. Also I do not see your Exe file either.

Thank you.

Hi,


When you reply to this post, you will see “Add/Update” button at the bottom, you may use this button to attach your zipped files or other attachments.

Thank you.

Hi,


when i try to save the zipped folder i got error message.

Please give your E-mail ID I will send it to your mail.


Thanks, Velkumar

Hi,


Please make sure to login in Aspose site before posting your reply with attachments in the forum thread(s).

Thank you.
Hi,

That redirected path when i clicked the save button is blocked here so error occurs.

So please give your mail ID.



This is my code
=============================

using System;
using Aspose.Cells;
using System.Data;


namespace AsposeCellsDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to create CSv file:");
Console.ReadLine();

var workBook = new Workbook();
Worksheet worksheet = workBook.Worksheets[0];

DataTable dataTable = new DataTable();

dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
dataTable.Columns.Add(new DataColumn("Home Address", typeof(string)));
dataTable.Columns.Add(new DataColumn("Communication Address", typeof(string)));
dataTable.Columns.Add(new DataColumn("Phone number Mobile or land Line", typeof(string)));

DataRow dataRow1 = dataTable.NewRow();

dataRow1[0] = "Velkumar";
dataRow1[1] = "Kumarakovil kanyakumari";
dataRow1[2] = "Kumarakovil Nagercoil kanyakumari";
dataRow1[3] = "456546465465";

DataRow dataRow2 = dataTable.NewRow();

dataRow2[0] = "Velkumar Kannan";
dataRow2[1] = "Kumarakovil kanyakumari tamilnadu";
dataRow2[2] = "Kumarakovil Nagercoil kanyakumari India";
dataRow2[3] = "353544536453644";

dataTable.Rows.Add(dataRow1);
dataTable.Rows.Add(dataRow2);

worksheet.Cells.ImportDataTable(dataTable, true, "A1");
worksheet.Cells.StandardWidth = 80.5;
workBook.Save("E:\\Demo.csv");

Console.WriteLine("Successfully created in E drive.");
Console.WriteLine("==================================================");
Console.WriteLine("==================================================");
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
}
}

Hi,


Thanks for the sample code.

I have evaluated your issue a bit. Well, you cannot specify the Column’s width / standard width or set auto-fit columns or rows in a CSV file. This is MS Excel’s CSV file format limitation and you cannot save changes if you set auto-fit columns/rows operations in it, you may confirm it in MS Excel. As CSV is just a string format so you cannot save the formattings or other things.

For your needs, you have to save to XLS or XLSX or other MS Excel formats for your requirements, e.g. you may save the file to XLSX, see the changed line:
workBook.Save(“E:\Demo.xlsx”);

Now when you open the file into MS Excel, it would contain the columns widths set as per your codes.

Hope, you understand now.

Thank you.


Hi,


I have tried using xlsx but the problem still occurs.

Thanks,
Velkumar.

Hi,


It works absolutely fine with latest version/fix i.e. v7.5.1.x, please try it if you are not using it already.

Here is my code and please find attached the output XLSX file which is fine tuned.

Sample code:

Console.WriteLine(“Press any key to create CSv file:”);
Console.ReadLine();

var workBook = new Workbook();
Worksheet worksheet = workBook.Worksheets[0];

DataTable dataTable = new DataTable();

dataTable.Columns.Add(new DataColumn(“Name”, typeof(string)));
dataTable.Columns.Add(new DataColumn(“Home Address”, typeof(string)));
dataTable.Columns.Add(new DataColumn(“Communication Address”, typeof(string)));
dataTable.Columns.Add(new DataColumn(“Phone number Mobile or land Line”, typeof(string)));

DataRow dataRow1 = dataTable.NewRow();

dataRow1[0] = “Velkumar”;
dataRow1[1] = “Kumarakovil kanyakumari”;
dataRow1[2] = “Kumarakovil Nagercoil kanyakumari”;
dataRow1[3] = “456546465465”;

DataRow dataRow2 = dataTable.NewRow();

dataRow2[0] = “Velkumar Kannan”;
dataRow2[1] = “Kumarakovil kanyakumari tamilnadu”;
dataRow2[2] = “Kumarakovil Nagercoil kanyakumari India”;
dataRow2[3] = “353544536453644”;

dataTable.Rows.Add(dataRow1);
dataTable.Rows.Add(dataRow2);


worksheet.Cells.ImportDataTable(dataTable, true, “A1”);

worksheet.Cells.StandardWidth = 80.5;

workBook.Save(“E:\test2\outDemo1.xlsx”);

Console.WriteLine(“Successfully created in E drive.”);
Console.WriteLine("==================================================");
Console.WriteLine("==================================================");
Console.WriteLine(“Press any key to exit”);
Console.ReadLine();