2013-10-11 26 views
12

我的代码会产生这样的如何格式化使用C#在Excel/CSV标题

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

Excel文档,但我想是这样的...

----------------------------------------------------- 
| 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

你知道为什么你是从别人收到downvote?由于这个原因:“你写的代码的问题必须在问题本身中描述具体问题 - 并包含有效的代码以再现它。”所以,展示你现在拥有的东西。 –

+0

你在使用Interop吗?如果是,请参阅[这](http://www.siddharthrout.com/vb-dot-net-and-excel/)检查“合并/取消合并单元格”部分代码在vb中。净,但可以轻松移植到C# –

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

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

    <!#> 
    <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文件的内容,你将与<#!它拆分>和繁殖的数据行的一部分,追加一起,保存到另一个file..thats它..

-5

您需要安装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 
+0

这个回答有问题吗? – Fedor

+0

这告诉你,Microsoft Visual Studio Tools for Office中有很多内置功能,因此您可以使用合并属性来执行所需的格式设置。 – KhanZeeshan

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