C# 如何使用C在excel/csv中格式化标题#

C# 如何使用C在excel/csv中格式化标题#,c#,.net,csv,export-to-excel,C#,.net,Csv,Export To Excel,我的代码将生成这样的excel文档 |id | Name | Address | company_Name | Destination| |----|-------|----------|--------------|------------| |##1 | xxx | xxxx | xxx | xxxxx | 但我想要这样… ----------------------------------------------------- | Perso

我的代码将生成这样的excel文档

|id  | Name  | Address  | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx   | xxxx     | xxx          | xxxxx      |
但我想要这样…

-----------------------------------------------------
| Personal Information  |   Working INFO            |
-----------------------------------------------------
|id  | Name  | Address  | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx   | xxxx     | xxx          | xxxxx      |
-----------------------------------------------------
我从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");
}
没有问题

我想把标题作为内联的东西

  • 首先,在excel中创建excel文件模板,并根据需要设置格式
  • 只在打印多行的地方放一行
  • 将标签放入数据行以替换为真实数据
  • 另存为MHT文件。(样式也将像html一样保存)
  • 用文本编辑器打开mht文件
  • 放<!\35;>在数据行的开头和结尾,这样您就可以从程序中分割文件内容,并通过用真实属性替换标签来动态填充真实数据行。

    <!#>
    <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>
    <!#>
    
    
    [id]=
    [姓名]
    [公司]
    [目的地]=
    
  • 从文本编辑器保存文件

  • 从您的程序中,您将读取mht文件内容,您将使用<!#>然后将数据行部分相乘,全部追加并保存到另一个文件中..就是这样


  • 您需要已安装Microsoft Visual Studio Tools for Office

    之后,创建公共.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
    

    Csv格式不支持合并单元格,但您仍然可以按上述方式排列标题行。只需将分隔符替换为单元格分隔符即可。

    是否考虑过使用closedxml()


    您还可以打开csv并另存为csv

    您知道为什么收到某人的否决票吗?因此:
    有关您编写的代码问题的问题必须在问题本身中描述具体问题,并包括重现问题的有效代码。
    因此,请展示您现在的情况。忽略我的上述评论。我刚看到你的编辑。我的建议是以你想要的格式创建一个Excel文件,然后将其保存为csv。然后在记事本中打开它。查看数据是如何对齐的。只需在C#中创建它,您能否显示创建
    报告变量的代码部分
    ?String report=reportDocument.getExcelReport();&很抱歉延迟回答此问题?这说明Microsoft Visual Studio Tools for Office中有许多内置功能,因此您可以使用“合并”属性执行所需的格式设置。
    ...
    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.
    ...
    
     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);