如何将OpenXML放到剪贴板上,以便粘贴到Excel中? [英] How do I get OpenXML onto the clipboard so that it will paste into Excel?

查看:164
本文介绍了如何将OpenXML放到剪贴板上,以便粘贴到Excel中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft的DocumentFormat.OpenXML库生成OpenXml。我正在寻找如何将此文档导入到我的剪贴板,以便我可以将我的数据粘贴到Excel(好像从Excel复制)。当我从Excel中复制时,我可以看到从Excel中出来的OpenXml格式化数据。我需要做相反的,从WPF应用程序中复制,并使用高级Excel格式化(因此需要OpenXML)粘贴到Excel。



以下是截至目前为止的一段代码:

  MemoryStream documentStream = new MemoryStream(); 
SpreadsheetDocument spreadsheet = SpreadsheetDocument.Create(documentStream,SpreadsheetDocumentType.Workbook,true);

//创建工作簿
spreadsheet.AddWorkbookPart();
Stream workbookStream = spreadsheet.WorkbookPart.GetStream();
spreadsheet.WorkbookPart.Workbook = new Workbook(); //创建工作表
spreadsheet.WorkbookPart.AddNewPart< WorksheetPart>();
spreadsheet.WorkbookPart.WorksheetParts.First()。Worksheet = new Worksheet();

...

const string SPREADSHEET_FORMAT =XML Spreadsheet;
Clipboard.SetData(SPREADSHEET_FORMAT,clipboardData);


解决方案

我已经手动构建了XML(不是通过OpenXML) ,已被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: w3.org/TR/REC-html40\">
<样式>
< Style ss:ID =Defaultss:Name =Normal>
<对齐ss:垂直=底部/>
<边框/>
< Font />
< Interior />
< NumberFormat />
<保护/>
< / Style>
<样式ss:ID =s21>
< NumberFormat ss:Format =Currency/>
< / Style>
<样式ss:ID =s22>
<内部ss:Color =#FFFF00ss:Pattern =Solid/>
< / Style>
< / Styles>
<工作表ss:Name =Sheet2>
< Table ss:ExpandedColumnCount =256ss:ExpandedRowCount =65536
x:FullColumns =1x:FullRows =1ss:DefaultRowHeight =13.2>
< Row>
<单元格>
<数据ss:Type =String> test< / Data>
< / Cell>
<单元格ss:样式ID =s21>
<数据ss:Type =Number> 1.12< / Data>
< / Cell>
<单元格>
<数据ss:Type =Number> 1.1213< / Data>
< / Cell>
<单元格ss:样式ID =s21>
<数据ss:Type =Number> 12.12121< / Data>
< / Cell>
<单元格ss:StyleID =s22>
<数据ss:Type =String> test< / Data>
< / Cell>
< / Row>
< /表>
< / Worksheet>
< / Workbook>

将代码发送到剪贴板(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)


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.GetStream();
  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)

这篇关于如何将OpenXML放到剪贴板上,以便粘贴到Excel中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆