We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Converting to csv not working

Hi,

I am tring to convert a file from xls to csv but only the html text is downloaded not the data.

Can you please show me how to download the correct data to CSV from XLS?

Attached is my html code downloaded instead of the actual data.

Below is my code:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
sheet.Name = "expenses";

//Obtain the cells of the first worksheet.
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;

//Accessing a column from the Columns collection
Column columnCurrency = cells.Columns[1];

Aspose.Cells.Row rowHeader = cells.Rows[6]; //first row of data?

Aspose.Cells.Style style;
StyleFlag flag;
style = workbook.Styles[workbook.Styles.Add()];
style.Number = 7;
style.HorizontalAlignment = TextAlignmentType.Right;
flag = new StyleFlag();
flag.NumberFormat = true;
columnCurrency.ApplyStyle(style, flag);

sheet.Cells.ImportDataTable(dtData, false, "A8");
sheet.AutoFitColumns();

string _SaveAsFileName;
string fileXLS = "~/tempdata/xls2PDF.xls";
string fileCSV = "~/tempdata/xls2CSV.csv";
switch (downloadType)
{
case "csv":
_SaveAsFileName = SaveAsFileName + ".csv";

workbook.Save(Server.MapPath(fileXLS), FileFormatType.Excel2003);
workbook.Open(Server.MapPath(fileXLS)); //opens an xls file
workbook.Save(Server.MapPath(fileCSV), FileFormatType.CSV);
workbook.Save(_SaveAsFileName, FileFormatType.CSV, Aspose.Cells.SaveType.OpenInExcel, this.Response); //saves it as a csv file
break;

}

Hi,

After checking your code, I think you are saving multiple copies of the formats with a single step. It is to be noted here, when you call Workbook.Save() method, all the data and objects related to the workbook would be null. So, if you write code like this in the portion below it would not be worthwhile for you. You should open the workbook first then call the Save() method.

workbook.Save(Server.MapPath(fileXLS), FileFormatType.Excel2003);
workbook.Open(Server.MapPath(fileXLS)); //opens an xls file
workbook.Save(Server.MapPath(fileCSV), FileFormatType.CSV);
workbook.Save(_SaveAsFileName, FileFormatType.CSV, Aspose.Cells.SaveType.OpenInExcel, this.Response); //saves it as a csv file

I think you can try to change your code as follows:

workbook.Save(Server.MapPath(fileXLS), FileFormatType.Excel2003);
workbook.Open(Server.MapPath(fileXLS)); //opens an xls file
workbook.Save(Server.MapPath(fileCSV), FileFormatType.CSV);
workbook.Open(Server.MapPath(fileXLS)); //opens an xls file
workbook.Save(_SaveAsFileName, FileFormatType.CSV, Aspose.Cells.SaveType.OpenInExcel, this.Response); //saves it as a csv file


Thank you.

Hi Amjad,

Thanks for looking into this. After changing my code, now I got both the CSV data and the HTML in my download. How I do get rid of the HTMLs ?

Hi,

Which htmls you are talking about? Could you post your template XLS file here, we want to save it as .CSV file to check and reproduce the issue.

Thank you.

Hi,

The html is what is displayed on the web page. Basically, I have a webpage which displays some data and has a download button for the user to download the same data in the CSV, PDF or Excel format. Downloading to xls and PDF works well other than converting the xls to csv. The csv file downloaded contains both the data from the dataset and the contents displays on the webpage.

I attached the csv file for your review. Thanks!

Hi,

Thanks for providing us the template file.

Which version you are using? Could you try the attached version.

Thank you.

Hi,

This new version fixed the csv issue and created two new issues.

1. Converting xls to PDF is not working, the pdf is blank

2. Extra text got appended after the Evaluation License info. Please see below:


This file is created with Aspose.Cells 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.਍਍ℼ佄呃偙⁅瑨汭倠䉕䥌⁃ⴢ⼯㍗⽃䐯䑔堠呈䱍ㄠ〮吠慲獮瑩潩慮⽬䔯≎∠瑨灴⼺眯睷眮⸳牯⽧剔砯瑨汭⼱呄⽄桸浴ㅬ琭慲獮瑩潩慮⹬瑤≤ാഊ㰊瑨汭砠汭獮∽瑨灴⼺眯睷眮⸳牯⽧㤱㤹砯瑨汭•ാ㰊敨摡㰾楴汴㹥਍䄉灳獯⁥敔瑳瀠条൥㰊琯瑩敬㰾栯慥㹤਍戼摯㹹਍††昼牯慮敭∽潦浲∱洠瑥潨㵤瀢獯≴愠瑣潩㵮琢獥⹴獡硰•摩∽潦浲∱ാ㰊楤㹶਍椼灮瑵琠灹㵥栢摩敤≮渠浡㵥弢噟䕉南䅔䕔•摩∽彟䥖坅呓呁≅瘠污敵∽眯偅睄䭕呌硕呎㍍橍ㅅ睎欹杆䍉睁欹杆䍉允㠸睋乁䝁契允䩕㍒灊䙚灚塚硣㉄此晱⭱癓㉧睒䡯伶潮䝐汔㝸卵佑㴴•㸯਍⼼楤㹶਍਍††搼癩ാ †††㰠楤㹶਍਍⼼楤㹶਍††⼼楤㹶਍††搼癩ാ †††㰠湩異⁴祴数∽畳浢瑩•慮敭∽瑢䍮噓•慶畬㵥䌢噓•摩∽瑢䍮噓•㸯渦獢㭰渦獢㭰਍††††椼灮瑵琠灹㵥猢扵業≴渠浡㵥戢湴䑐≆瘠污敵∽䑐≆椠㵤戢湴䑐≆⼠☾扮灳☻扮灳഻ †††㰠湩異⁴祴数∽畳浢瑩•慮敭∽瑢塮卌•慶畬㵥堢卌•摩∽瑢塮卌•㸯਍††਍††⼼楤㹶਍††਍††਍搼癩ാഊऊ椼灮瑵琠灹㵥栢摩敤≮渠浡㵥弢䕟䕖呎䅖䥌䅄䥔乏•摩∽彟噅久噔䱁䑉呁佉≎瘠污敵∽眯坅䅂歌癳䵹兄噌火桇杄獌獫欳睁歌煫⭇啃昷さ㠫䍴䴴湷㕧晭杗婬坌䱵⽃•㸯਍⼼楤㹶⼼潦浲ാ㰊戯摯㹹਍⼼瑨汭ാ.

Hi,

1. Converting xls to PDF is not working, the pdf is blank

Kindly post your input .xls file here, we will check the issue soon.

2. Extra text got appended after the Evaluation License info. Please see below:

Thanks for pointing it out, We will figure it out soon.

Thank you.

thanks. Attached is my .xls file

Hi,

Well, I don't find any problem at all, the output .pdf file (attached) is fine.

Following is my testing code using your template file and attached is the output .pdf file.

Sample code:

string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "..\\..");
//Get the template excel file path.
string designerFile = path + @"\Files\xls2PDFSupport.xls";
//Specify the xml file path.
string xmlFile = path + @"\Files\xls2PDFSupport.xml";
//Specify the pdf file path.
string pdfFile = path + @"\Files\xls2PDFSupport.pdf";
//Create a new Workbook.
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();

FileStream fstream = new FileStream(designerFile, FileMode.Open);
wb.Open(fstream);
//Create a new Pdf object.
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
//Now, Bind the xml file (Aspose.Pdf integrated), which is
//used as a medium file for conversion.
pdf.BindXML(xmlFile, null);
//pdf.BindXML(ms, null);
//Save the pdf file.
pdf.Save(pdfFile);

And I used Aspose.Cells for .NET 4.5.0.14 and Aspose.Pdf for .NET 3.6.2.5

Thank you.

I downloaded Aspose.PDF version 3.7 and now it works.

Thanks for your help