我必须将数据网格值导出到Excel。我使用以下代码来复制datagrid值。
InvoiceDataGrid.SelectAllCells()
InvoiceDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader
ApplicationCommands.Copy.Execute(Nothing, Me.InvoiceDataGrid)
Dim result1 As String = Clipboard.GetData(DataFormats.Text)
InvoiceDataGrid.UnselectAllCells()
但有时它不会从网格中获取整数值。请帮我。 。
这是我在C#中编写和使用的东西......
ExportToExcel
private void ExportToExcel()
{
try
{
dataGrid.SelectAllCells();
dataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dataGrid);
var resultat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
var result = (string)Clipboard.GetData(DataFormats.Text);
dataGrid.UnselectAllCells();
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var excelFile = new StreamWriter(@"C:\Users\"+Environment.UserName+@"\Desktop\exportedcompanies.xls");
excelFile.WriteLine(result.Replace(',', ' '));
excelFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}