How do I get OpenXML onto the clipboard so that it will paste into Excel?
I am generating OpenXml using the DocumentFormat.OpenXML library from Microsoft. I am tring to figure out how to get this document into my clipboard so that I can paste my data into Excel (as if it were copied from Excel). I am able to see OpenXml formated data coming out of Excel, when I copy from within Excel. I need to do the reverse, copy out of a WPF application, and paste into Excel using advanced Excel formatting (hence needing OpenXML).
Here is a snippet of what I have so far:
MemoryStream documentStream = new MemoryStream();
SpreadsheetDocument spreadsheet = SpreadsheetDocument.Create(documentStream, SpreadsheetDocumentType.Workbook, true);
// create the workbook
spreadsheet.AddWorkbookPart();
Stream workbookStream = spreadsheet.WorkbookPart.GetS开发者_如何学运维tream();
spreadsheet.WorkbookPart.Workbook = new Workbook(); // create the worksheet
spreadsheet.WorkbookPart.AddNewPart<WorksheetPart>();
spreadsheet.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet();
...
const string SPREADSHEET_FORMAT = "XML Spreadsheet";
Clipboard.SetData(SPREADSHEET_FORMAT, clipboardData);
I've constructed XML manually (not through OpenXML), that has been accepted by excel.
Some interesting details about the header:
Notice the second line.
Full XML I'm sending (for testing):
<?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">
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s21">
<NumberFormat ss:Format="Currency"/>
</Style>
<Style ss:ID="s22">
<Interior ss:Color="#FFFF00" ss:Pattern="Solid"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet2">
<Table ss:ExpandedColumnCount="256" ss:ExpandedRowCount="65536"
x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="13.2">
<Row>
<Cell>
<Data ss:Type="String">test</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="Number">1.12</Data>
</Cell>
<Cell>
<Data ss:Type="Number">1.1213</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="Number">12.12121</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">test</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
Code to send it to clipboard (VB.Net):
Dim xml As String = File.ReadAllText("excel.xml")
Dim xmlStream As Stream = New MemoryStream()
xmlStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(xml), 0, xml.Length)
Clipboard.SetData("XML Spreadsheet", xmlStream)
You can't. I've been through this with Microsoft and Office will not accept an OpenXML format in the clipboard or drag/drop from any app other than Office. (They also claim Office itself does not use this format but ClipSpy shows that they do - in the flat package format.)
HTML is your best bet.
精彩评论