3

正如标题所说,我尝试将剪贴板中的内容实际粘贴到 Excel 中。

我有以下代码:

Clipboard.SetText(html);
sheet.Range("A1").Value = Clipboard.GetText(); 

实际上,变量 html 包含一个 html 代码文件,当我这样做时,我实际上只将 html 内容粘贴到 Range 中,但是,如果我打开 Excel 并手动执行,选择性粘贴...我可以粘贴 html代码,但它将代码转换为真实的表格而不是 html 代码,这是我想要的真实结果,无需手动完成。

Excel.Range.Copy() 使用 Clipboard.GetText() 粘贴

另一种方法是:

foreach (Excel.Worksheet sheet in workbook.Sheets)
{
    foreach (Excel.Shape shape in sheet.Shapes)
    {
        Clipboard.SetText(html);

        //doesn't work:                              
        sheet.Range("A1").Value = sheet.PasteSpecial(Clipboard.GetText()); 

        sheet.PasteSpecial(Clipboard.GetText()); //throws error
    }
}

但这种方式也行不通。我可以使用 html -> 图像并粘贴图像,但应该可以访问实际值而不是图片。

希望有人能澄清如何解决它。

谢谢。

4

5 回答 5

3

您可以尝试使用SetData而不是SetText

 Clipboard.SetData(DataFormats.Html, html);

将字符串复制到剪贴板并将其标记为 HTML(如果这在您的情况下不起作用,SetText可能没问题)。

PasteSpecial考虑到您的评论,调用您希望插入的单元格范围:

ActiveSheet.Range("A1").PasteSpecial(
         Excel.Enums.XlPasteType.xlPasteAll,
         Excel.Enums.Xl‌​PasteSpecialOperation.xlPasteSpecialOperationNone,
         false, false);

请注意,使用该Value属性为单元格分配新值永远不会复制任何格式等。

于 2013-03-27T08:56:48.467 回答
1

我简单的 C# 复制和粘贴(从 Input/Sheet[1] 到 Output/Sheet[1])

using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

// prepare Input
        Excel.Application xlApp = new Excel.Application();
        xlApp.DisplayAlerts = false;
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileNameIn);
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;      //I copy everything
// prepare Output
        Excel.Application oXL = new Excel.Application();
        oXL.DisplayAlerts = false;
        Excel.Workbook mWorkBook = oXL.Workbooks.Open(fileNameOut, 0, false, 5,
                                "", "", false, Excel.XlPlatform.xlWindows,
                                "", true, false, 0, true, false, false);
        Excel.Worksheet mWSheet1 = mWorkBook.Sheets[1];
// make Copy&Paste in PC memory
        xlRange.Copy(Type.Missing);
        Excel.Range targetRange = mWSheet1.Cells[11, 1];   //initial cell for Paste
        mWSheet1.Paste(targetRange);
// save Output
        mWorkBook.SaveAs(fileNameOut, Excel.XlFileFormat.xlWorkbookNormal, 
                                Missing.Value, Missing.Value,
                                Missing.Value, Missing.Value,  
                                Excel.XlSaveAsAccessMode.xlExclusive,
                                Missing.Value, Missing.Value,
                                Missing.Value, Missing.Value, Missing.Value);
// clean the waste !
        mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
        mWSheet1 = null; mWorkBook = null; oXL.Quit();
        GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers();
        GC.Collect(); GC.WaitForPendingFinalizers();
        Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(xlWorksheet);
        xlWorkbook.Close(); Marshal.ReleaseComObject(xlWorkbook);
        xlApp.Quit(); Marshal.ReleaseComObject(xlApp);
于 2019-02-28T11:30:10.103 回答
1

使用这个:xlWorkSheet.PasteSpecial(Missing.Value, false, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

//下面是完整代码

            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.Visible = true;
            xlApp.UserControl = true;
            xlApp.WindowState = Excel.XlWindowState.xlMaximized;
            xlWorkBook = xlApp.Workbooks.Add(Type.Missing);
           // xlWorkBook = xlApp.Workbooks.Open(excel_filename);
            xlApp.ActiveWorkbook.Sheets[1].Activate();
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.PasteSpecial(Missing.Value, false, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            xlWorkBook.SaveAs(save_file_name);
            Console.WriteLine("saved file name" + save_file_name);
            xlWorkBook.Close();
            xlApp.Quit();
于 2015-09-04T12:48:09.587 回答
1

我设法通过使用从剪贴板中获取复制的单元格

    Clipboard.GetData("XML Spreadsheet");

然后,在我通过我的代码做了一些复制粘贴之后,我把它放回去了

    Clipboard.SetData("XML Spreadsheet", originalObject);

快乐编码!

于 2016-05-23T14:07:26.677 回答
0

此代码用于将 html 字符串转储到 excel 中

using Microsoft.Office.Interop.Excel;

private void ExcelExport()
{
    var excel = new Application { Visible = true };
    excel.WindowState = XlWindowState.xlMaximized;
    Workbook workbook = excel.Workbooks.Add(XlSheetType.xlWorksheet);
    Worksheet sheet = workbook.Sheets[1];
    sheet.Activate();

    string s = "<html><body><table>";

    for (int i = 1; i <= 100; i++)
    {
        s += "<tr>";

        for (int f = 1; f <= 100; f++)
        {
            s += "<td>" + i.ToString() + "," + f.ToString() + "</td>";
        }

        s += "</tr>";
    }

    s += "</table></body></html>";

    System.Windows.Forms.Clipboard.SetText(s);

    sheet.Range["A1"].Select();
    sheet.PasteSpecial(Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    sheet.Range["A1"].Select();
}

于 2018-03-14T11:11:43.860 回答