In: Computer Science
Hi!
How I can convert from xml file to xlsx(excel ) sheet using Aspose.Cells, in C#
I Try with this code but I need loop , so every element is one row with elements id (name )in column 1, and texten in column 2.
Workbook wb = new workbook();
worksheet ws = wb.worksheets[0];
ws.Cells[0,0].putValue("name");
wb.Save(path, "xml_to_exce.xlsxl");
Thanks!
Please find the code below let me know if you have any queries.
using System;
using System.Data;
using System.IO;
using Aspose.Cells;
namespace Assignment1
{
class Program
{
static void Main(string[] args)
{
// Read XML file into DataSet Object
// Note
DataSet ds = new DataSet();
FileStream fs = new FileStream("XmlFilePath", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
ds.ReadXml(reader);
fs.Close();
// Get the data into DataTable Variable
DataTable dt = ds.Tables[0];
// Load Aspose License
Aspose.Cells.License li = new Aspose.Cells.License();
li.SetLicense("Aspose_License_path"); //Cracking Certificate
// Create New Work book and load data into workbook by looping over
// DataTable Rows and Columns
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
int Colnum = dt.Columns.Count;
int Rownum = dt.Rows.Count;
for(int i=0; i<Colnum; i++)
{
cells[0, i].PutValue(dt.Columns[i].ColumnName);
}
for(int i=0; i < Rownum; i++)
{
for(int j = 0; j<Colnum; j++)
{
cells[1 + i, j].PutValue(dt.Rows[i][j].ToString());
}
}
//Save Workbook
workbook.Save("workbook_filepath");
}
}
}