我正在使用以下代码从DataGridView
Excel 文件中复制数据,然后粘贴到其中。
private void copyAlltoClipboard()
{
//to remove the first blank column from datagridview
dataGridView1.RowHeadersVisible = false;
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
private void button3_Click_1(object sender, EventArgs e)
{
copyAlltoClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select(); // CR is a COM Object
// WorkSheet.PasteSpecial(object,object,object,object,object);
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
这会在 excel 文件中产生这个结果:
它在几乎所有机器上运行良好,但在生产中的少数机器上,它粘贴为黑点图片(我猜它正在将数据转换为位图表示)。这是它粘贴在某些机器上的内容:
我尝试使用函数PasteSpecial()
。但是它不会在任何机器上复制任何东西。我不确定这里出了什么问题。
在运行良好和不运行的机器中,我找不到配置方面的差异。知道如何处理这个问题并在所有机器上显示数据而不是图片吗?