Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format heading in excel / csv using C#

My code will generate the excel document like this

|id  | Name  | Address  | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx   | xxxx     | xxx          | xxxxx      |

But I want like this...

-----------------------------------------------------
| Personal Information  |   Working INFO            |
-----------------------------------------------------
|id  | Name  | Address  | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx   | xxxx     | xxx          | xxxxx      |
-----------------------------------------------------

I get the data from the API and I will save it using the SaveFileDialog

SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = "Save file as...";
dialog.Filter = "Text files (*.csv)|*.csv";
dialog.RestoreDirectory = true;

if (dialog.ShowDialog() == DialogResult.OK)
{
     System.IO.StreamWriter writer = new System.IO.StreamWriter(dialog.FileName); //open the file for writing.
     writer.Write(report); //write the current date to the file. change this with your date or something.
     writer.Close(); //remember to close the file again.
     writer.Dispose(); //remember to dispose it from the memory.

     program.ShowInformationMessage("File Save successfully");
}

There is no problem with it.

I want to make the heading as the inline something like it.

like image 436
GOPI Avatar asked Oct 11 '13 07:10

GOPI


People also ask

Can CSV files have headers?

A header of the CSV file is an array of values assigned to each of the columns. It acts as a row header for the data. Initially, the CSV file is converted to a data frame and then a header is added to the data frame. The contents of the data frame are again stored back into the CSV file.

What does the C in CSV stand for?

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas.


2 Answers

...
System.IO.StreamWriter writer = new System.IO.StreamWriter(dialog.FileName); //open the file for writing.
writer.Write("Personal Information" + delimiter + delimiter + "Working INFO" + delimiter);
writer.Write(report); //write the current date to the file. change this with your date or something.
...

Csv format doesn't support merged cells, but still you can arrange header line like described above. Just replace delimiter with whatever your cell delimiter is.

like image 71
bigkahunaburger Avatar answered Oct 13 '22 19:10

bigkahunaburger


Have you considered using closedxml (https://closedxml.codeplex.com/).

 var wb = new XLWorkbook(report); //open spreadsheet
 IXLWorksheet ws = wb.Worksheets.First(); //get first sheet
 ws.Row(1).InsertRowsAbove(1); //insert row
 ws.Cell("A1").Value = "Personal Information";
 ws.Cell("A4").Value = " Working INFO";
 ws.Range("A1:A3").Row(1).Merge(); // merge first title
 ws.Range("A4:A6").Row(1).Merge(); // merge second
 wb.SaveAs(writer);

You can also open csv's and save as csv

like image 26
Gus van Diermen Avatar answered Oct 13 '22 20:10

Gus van Diermen