6

新しい XML スプレッドシートを作成するなど、データを Xml 形式の Excel シートにエクスポートするなどの要件があります。Excel xml スプレッドシートを作成するには、このリンクをたどりました。このリンクでは、彼はサンプルについて言及しています

< ?xml version="1.0"?>
< ?mso-application progid="Excel.Sheet"?>
<workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<documentproperties xmlns="urn:schemas-microsoft-com:office:office">
<author>Author</author>
<lastauthor>LastAuthor</lastauthor>
<created>11-09-2007</created>
<version>12.00</version>
</documentproperties>
<excelworkbook xmlns="urn:schemas-microsoft-com:office:excel">
<protectstructure>False</protectstructure>
<protectwindows>False</protectwindows>
</excelworkbook>
</workbook>

C#プロジェクトでこの形式を定義する必要がある場所、上記のコードでは、作成者に関する情報を取得する必要があり、最後の作成者はデータベースからバインドする必要があります....

そのリンクでは、ドキュメントの作成について完全には言及していません...

ExcelXml spread sheet従う必要がある手順を作成したい場合、プロジェクトに保存される定義済みの形式を作成する必要がありますか?

オープン XML SDK にアクセスできますが、Excel スプレッドシート内で xml 形式を作成するためのサンプル ソリューションが見つかりませopen XML SDKん。

誰かが私に非常に感謝するアイデアや解決策を持っていますか....

よろしくお願いします

4

2 に答える 2

1

テンプレートに失敗した場合は、ここから取得した以下を試してください

using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

public static void CreateSpreadsheetWorkbook(string filepath)
    {
        // Create a spreadsheet document by supplying the filepath.
        // By default, AutoSave = true, Editable = true, and Type = xlsx.

        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

        // Add a WorkbookPart to the document.
        WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
        workbookpart.Workbook = new Workbook();

        // Add a WorksheetPart to the WorkbookPart.
        WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet(new SheetData());

        // Add Sheets to the Workbook.
        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

        // Append a new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
        sheets.Append(sheet);

        workbookpart.Workbook.Save();

        // Close the document.
        spreadsheetDocument.Close();
    }

// Called using
CreateSpreadsheetWorkbook("C:\\Test\\Test.xlsx");

編集:次のコードを使用して、xml を Excel に変換できます。

Workbook workbook = new Workbook(); 
workbook.LoadFromFile(@"../../Data/test.xml"); 
workbook.SaveToFile(@"..\..\result.xlsx", ExcelVersion.Version2010);

実際に Office XML ドキュメントを作成したい場合、xml ファイルからそのプロセスを自動化する方法がわかりません。いくつかの指針についてこれを見てください

于 2013-09-21T07:48:27.683 に答える