[英]How to format heading in excel / csv using C#
My code will generate the excel document like this 我的代码将生成这样的excel文档
|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
我从API获取数据,然后使用
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. 我想将标题设为内联类似的东西。
...
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. Csv格式不支持合并单元格,但仍然可以像上面描述的那样排列标题行。 Just replace delimiter with whatever your cell delimiter is.
只需用您的单元格分隔符替换分隔符。
Have you considered using closedxml ( https://closedxml.codeplex.com/ ). 您是否考虑过使用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 你也可以打开csv并保存为csv
Put < !#> at the begining and end of the data row so that you can split the file content from your program and populate real data lines dynamically by replacing your labels with real properties. 将<!#>放在数据行的开头和结尾,这样您就可以从程序中拆分文件内容,并通过用真实属性替换标签来动态填充实际数据行。
<!#> <tr height=3D20 style=3D'height:15.0pt'> <td height=3D20 class=3Dxl67 style=3D'height:15.0pt;border-top:none'>[id]=</td> <td class=3Dxl67 style=3D'border-top:none;border-left:none'>[name]</td> <td class=3Dxl67 style=3D'border-top:none;border-left:none'>[company<span style=3D'display:none'>]</span></td> <td class=3Dxl67 style=3D'border-top:none;border-left:none'>[destination]=</td> </tr> <!#>
Save the file from text editor. 从文本编辑器中保存文件。
From your program, you will read the mht file content, you will split it with the < !#> and multiply the data row part, append all together and save to another file..thats it.. 从您的程序中,您将读取mht文件内容,您将使用<!#>将其拆分并将数据行部分相乘,将所有内容附加在一起并保存到另一个文件中。
You need to have installed Microsoft Visual Studio Tools for Office. 您需要安装Microsoft Visual Studio Tools for Office。
After that create common .NET project and add the reference to COM object Microsoft.Office.Interop.Excel.dll via 'Add Reference..' dialog. 之后,创建常见的.NET项目,并通过“添加引用...”对话框添加对COM对象Microsoft.Office.Interop.Excel.dll的引用。
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);
//Get All available worksheets
//Excel.Sheets excelSheets = wb.Worksheets;
//Get Specific WorkSheet
string currentSheet = "Sheet1";
Excel.Worksheet newSheet = (Excel.Worksheet)wb.get_Item(currentSheet);
newSheet.Cells[i, j].HorizontalAlignment = ExcelAlignment.xlLeft; //or Excel.XlHAlign.xlHAlignLeft
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.