13

私のコードは、このような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

4 に答える 4

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 形式は結合されたセルをサポートしていませんが、上記のようにヘッダー行を配置できます。区切り文字をセル区切り文字に置き換えるだけです。

于 2014-01-29T19:38:07.383 に答える
-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
于 2014-01-27T07:42:15.940 に答える