C# 如何使用C在excel/csv中格式化标题#
我的代码将生成这样的excel文档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
|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");
}
没有问题
我想把标题作为内联的东西
<!#>
<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]=
[姓名]
[公司]
[目的地]=
您需要已安装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);