【发布时间】:2019-12-06 17:02:48
【问题描述】:
我的excel数据如下:
项目 |人 |时间 |日期
支持 |一个 |50 | 2019-10-10
信息技术 | B |1,20 |2019-10-10
调试 | A |30 |2019-10-11
支持 | c |20 |2019-10-11
支持 | A |30 |2019-10-12
信息技术 |乙| 1.20 |2019-10-12
在我的代码中,我可以从 datagridview 导出所有 excel 数据,如何只导出一列。我想导出列项目并对每种类型的项目进行排序并求和,例如导出的新 excel 文件应如下所示:
2019 年 10 月 10 日至 2019 年 10 月 12 日
项目 |总和
支持 | 1.40
信息技术 | 2.40
调试 | 30
这是我的导出代码。你能帮帮我吗?
谢谢你的帮助。
private void copyAlltoClipboard()
{
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
private void Button4_Click(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();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
【问题讨论】:
-
不要使用 Interop - 那是一种压力。看看这个:stackoverflow.com/questions/151005/…