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

时间:2013-10-11 07:24:57

标签: c# .net csv export-to-excel

我的代码会像这样生成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");
}

它没有问题。

我想将标题设为类似内联的内容。

4 个答案:

答案 0 :(得分:2)

...
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格式不支持合并单元格,但仍然可以像上面描述的那样排列标题行。只需用你的单元格分隔符替换分隔符。

答案 1 :(得分:2)

您是否考虑过使用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);

您也可以打开csv并保存为csv

答案 2 :(得分:-3)

  1. 首先在excel中创建excel文件模板,然后根据需要对其进行格式化。
  2. 只放一行会打印许多行。
  3. 将标签放入数据行以替换为真实数据。
  4. 将其另存为MHT文件。 (样式将被保存,也像html一样)
  5. 使用文本编辑器打开mht文件。
  6. Put< #!>在数据行的开头和结尾处,以便您可以从程序中拆分文件内容,并通过用真实属性替换标签来动态填充实际数据行。

    <!#>
    <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>
    <!#>
    
  7. 从文本编辑器中保存文件。

  8. 从您的程序中,您将读取mht文件内容,您将使用&lt; #!&GT;并将数据行部分相乘,将所有数据追加并保存到另一个文件中。然后它就可以了..

答案 3 :(得分:-5)

您需要安装Microsoft Visual Studio Tools for Office。

之后创建常见的.NET项目,并通过“Add Reference ..”对话框添加对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