Weird content in the clipboard after copy from Excel

◇◆丶佛笑我妖孽 提交于 2021-02-19 02:44:10

问题


I'm trying to get the content of the clipboard in my application to ensure excel compatibility, using :

Clipboard clipboard = new Clipboard(Display.getDefault());
String contents = (String) clipboard.getContents(TextTransfer.getInstance());

The problem is, if I have on one column [1, 2, 3], and I select cells [1] and [3], then in the clipboard content I found

1\r\n2\r\n3\r\n

instead of just 1 and 3. In other words the clipboard does not seem to handle disjointed cells.

Does somebody have an idea of what is going on ?


回答1:


I don't believe this is possible outside of Excel for a couple of reasons:

  1. As @Baz pointed out, text editors also respond by printing the contents of all three cells. Even Word does this.

    For example, say I have a spreadsheet open in Excel with the following contents:

    And then in Word I insert a new Excel spreadsheet:

    When we attempt to copy and paste only the cells containing A and C, we get the expected result:

    However, if Excel is not open when the new spreadsheet is added to the Word document, we get:

    To me this seems to indicate that when Excel is still open when the spreadsheet is added in Word, the instance of Excel is shared somehow and that's the only way it knows which cells to paste. (I'm using Office 365)

  2. As far as I can tell, Excel does not put anything into the clipboard to indicate which cells are selected. That would make sense given the results of pasting into Word after Excel has been closed, but to further the point this is the XML that Excel puts in the clipboard when only the cells containing A and C are copied:

<?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 ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
            <Interior/>
            <NumberFormat/>
            <Protection/>
        </Style>
    </Styles>
    <Worksheet ss:Name="Sheet1">
        <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="3"
        ss:DefaultRowHeight="15">
            <Row>
                <Cell><Data ss:Type="String">A</Data></Cell>
            </Row>
            <Row>
                <Cell><Data ss:Type="String">B</Data></Cell>
            </Row>
            <Row>
                <Cell><Data ss:Type="String">C</Data></Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

Notice that the data for all three cells is present in the clipboard contents, and there is nothing to indicate which cells are selected.

To get this XML, I extended the ByteArrayTransfer class for the "XML Spreadsheet" type to just write to a file:

public class ExcelTransfer extends ByteArrayTransfer {

    private static final String TYPE_NAME = "XML Spreadsheet";
    private static final int TYPE_ID = registerType(TYPE_NAME);

    private static final ExcelTransfer _instance = new ExcelTransfer();

    private ExcelTransfer() {
    }

    /**
     * @return
     */
    public static ExcelTransfer getInstance() {
        return _instance;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected int[] getTypeIds() {
        return new int[] { TYPE_ID };
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String[] getTypeNames() {
        return new String[] { TYPE_NAME };
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void javaToNative(final Object object, final TransferData transferData) {
        // ...
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Object nativeToJava(final TransferData transferData) {
        if (!isSupportedType(transferData)) {
            return null;
        }

        final byte[] buffer = (byte[]) super.nativeToJava(transferData);
        if (buffer == null) {
            return null;
        }

        final File f = new File("excel_clipboard.xml");
        try {
            f.createNewFile();
            final FileOutputStream fo = new FileOutputStream(f);
            fo.write(buffer);
            fo.close();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

With the cells already copied, run with:

public static void main(final String... args) {
    new Clipboard(Display.getDefault()).getContents(ExcelTransfer.getInstance());
}


来源:https://stackoverflow.com/questions/42906077/weird-content-in-the-clipboard-after-copy-from-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!