2013-09-21 59 views
6

Chúng tôi đã có yêu cầu như xuất dữ liệu vào bảng excel ở định dạng Xml như tạo Bảng tính XML mới Tôi đã theo liên kết này để tạo excel xml Spreadsheet. Trong liên kết này, ông đã đề cập đến mẫucác bước để tạo bảng tính XML excel bằng cách sử dụng C#

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

mà tôi cần phải xác định định dạng này trong # dự án c, Trong đoạn mã trên tôi cần để có được các thông tin về tác giả và tác giả cuối cùng cần phải ràng buộc từ cơ sở dữ liệu ....

trong đó liên kết ông đã không được đề cập hoàn toàn để tạo tài liệu ...

Nếu tôi muốn tạo một ExcelXml spread sheet là những bước mà tôi cần phải làm theo, làm tôi cần phải tạo ra một định dạng được xác định trước đó sẽ được lưu trữ trong dự án ...

chúng tôi có thể truy cập vào sdk XML mở, nhưng tôi tìm thấy bất kỳ giải pháp mẫu nào để tạo định dạng xml bên trong bảng tính excel, có thể làm điều tương tự với open XML SDK và nếu có thể, bạn sẽ chỉ cho tôi đúng hướng. ..

sẽ bất kỳ ai có ý tưởng và bất kỳ giải pháp mà sẽ rất biết ơn đối với tôi ....

Nhiều Cảm ơn trước

+1

Bạn có thể có một bảng tính mẫu và chỉ cần sao chép nó, nếu một mẫu sẽ là lý tưởng tất nhiên. –

+0

Hầu hết thời gian chúng ta chỉ tạo một speadsheet cơ sở và thay đổi nó bằng XSLT. –

+0

Tôi khuyên bạn nên sử dụng một cái gì đó như ClosedXML. Việc cố gắng sử dụng API OpenXML là khủng khiếp. – aquinas

Trả lời

0

Không một mẫu thử sau đây được lấy từ here

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"); 

EDIT: Bạn có thể chuyển đổi xml vượt trội sử dụng đoạn mã sau:

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

Nếu bạn muốn thực sự tạo ra một doc Văn phòng XML, tôi không chắc chắn như thế nào để tự động hóa quá trình đó từ một file xml. Hãy xem this for some pointers

+0

là nó tạo ra các định dạng xml dữ liệu bên trong excel tờ –

+0

cảm ơn cho rằng, một nghi ngờ là như nơi tôi cần phải đặt tất cả những cái này là nó trong tập tin lớp c hoặc là nó trong tập tin xml ... ' ' –

+0

để xuất, tôi có cần phải tạo định dạng xml trước và sau đó xuất thành bảng excel .. theo cách đó đúng ... –

0

Bạn có thể sử dụng SDK OpenXml cho tác vụ này.

Sử dụng SDK OpenXml trực tiếp không dễ dàng, đối với một ứng dụng đơn giản, bạn nên sử dụng trình bao bọc.

Hãy xem dự án JumboExcel (tiết lộ: Tôi là tác giả).

Tạo một bảng tính là dễ dàng như sau:

var tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".xlsx"); 
using (var file = new FileStream(tempFileName, FileMode.CreateNew)) 
{ 
    OpenXmlBuilder.Write(
     file, 
     new[] { 
      new Worksheet(
       "Parameters", 
       null, 
       new Row(new InlineString("Name"), new InlineString("Value")), 
       new Row(new InlineString("Height"), new DecimalCell(123m)) 
      ) 
     } 
    ); 
} 
Process.Start(tempFileName); 

Ngoài ra, bạn có thể khám phá các nguồn xem các nguồn tại Github page và hãy nhìn vào DemoTests để biết thêm ví dụ.

Các vấn đề liên quan