C# winform程序,修改在dataGridView中显示的Excel文件内容

chenhejlu 2016-10-26 09:43:29
大家好,我用C# winform的dataGridView控件读取并显示了excel2010文件。现在想设置一个按钮,每按一下,显示的excel中某些位置自动添加一些数值,数值来自几个Combobox控件。请问能否实现,如果可以的话,请有经验的朋友提供一下帮助,谢谢!
...全文
356 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhejlu 2016-10-27
  • 打赏
  • 举报
回复
自己顶一下,请有经验的朋友提供一下思路和建议,谢谢
chenhejlu 2016-10-27
  • 打赏
  • 举报
回复
引用 2 楼 hrsww 的回复:
在winform里拖入一个datagridview控件,跟一个openfiledialog控件 复制代码 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Microsoft.Office.Core; using Excel=Microsoft.Office.Interop.Excel; using System.Windows.Forms; using System.Reflection; namespace Excelproject { public partial class ExcelForm : Form { private ExcelOperate eo; private string excelpath; private Excel.Application excel1; private Excel.Workbooks wbs = null; private Excel.Workbook wb = null; private Excel.Sheets wss; private Excel.Worksheet ws = null; private Excel.Range range1 = null; public ExcelForm() { InitializeComponent(); this.excel1 = new Excel.Application(); if (excel1 == null) { MessageBox.Show("error"); System.Windows.Forms.Application.Exit(); } excel1.Visible = true; } #region excel文件打开关闭操作 private void 打开_Click(object sender, EventArgs e) { openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "打开excel文件"; openFileDialog1.Filter = "excel03文件(*.xls)|*.xls|excel07文件(*.xlsx)|*.xlsx"; openFileDialog1.InitialDirectory = @"C:\Users\Administrator\Desktop"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { //打开文件对话框选择的文件 excelpath = openFileDialog1.FileName; eo = new ExcelOperate(); readExcel(excelpath); } } void readExcel(string path) { object miss = System.Reflection.Missing.Value; excel1.UserControl = true; excel1.DisplayAlerts = false; excel1.Application.Workbooks.Open(excelpath, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss); wbs = excel1.Workbooks; wss = wbs[1].Worksheets; ws = (Excel.Worksheet) wss.get_Item(1); int rowNum = ws.UsedRange.Cells.Rows.Count; int colNum = ws.UsedRange.Cells.Columns.Count; string cellStr = null; char ch = 'A'; for (int i = 0; i < colNum; i++) { dataGridView1.Columns.Add(i.ToString(), ch.ToString()); dataGridView1.Rows.Add(rowNum); for (int j = 0; j <rowNum; j++) { cellStr = ch.ToString() + (j + 1).ToString(); dataGridView1[i, j].Value = ws.UsedRange.Cells.get_Range(cellStr, miss).Text.ToString(); } ch++; } } #endregion } }
谢谢你的回复,暂时的解决方法是编辑dataGridView控件的列,这样通过点击按钮,控件中特定位置赋值会容易一些,然后将空间内容另存为excel文件。
hrsww 2016-10-27
  • 打赏
  • 举报
回复
也在学习中,其它的自由发挥一下吧。
hrsww 2016-10-27
  • 打赏
  • 举报
回复
在winform里拖入一个datagridview控件,跟一个openfiledialog控件 复制代码 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Microsoft.Office.Core; using Excel=Microsoft.Office.Interop.Excel; using System.Windows.Forms; using System.Reflection; namespace Excelproject { public partial class ExcelForm : Form { private ExcelOperate eo; private string excelpath; private Excel.Application excel1; private Excel.Workbooks wbs = null; private Excel.Workbook wb = null; private Excel.Sheets wss; private Excel.Worksheet ws = null; private Excel.Range range1 = null; public ExcelForm() { InitializeComponent(); this.excel1 = new Excel.Application(); if (excel1 == null) { MessageBox.Show("error"); System.Windows.Forms.Application.Exit(); } excel1.Visible = true; } #region excel文件打开关闭操作 private void 打开_Click(object sender, EventArgs e) { openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "打开excel文件"; openFileDialog1.Filter = "excel03文件(*.xls)|*.xls|excel07文件(*.xlsx)|*.xlsx"; openFileDialog1.InitialDirectory = @"C:\Users\Administrator\Desktop"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { //打开文件对话框选择的文件 excelpath = openFileDialog1.FileName; eo = new ExcelOperate(); readExcel(excelpath); } } void readExcel(string path) { object miss = System.Reflection.Missing.Value; excel1.UserControl = true; excel1.DisplayAlerts = false; excel1.Application.Workbooks.Open(excelpath, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss); wbs = excel1.Workbooks; wss = wbs[1].Worksheets; ws = (Excel.Worksheet) wss.get_Item(1); int rowNum = ws.UsedRange.Cells.Rows.Count; int colNum = ws.UsedRange.Cells.Columns.Count; string cellStr = null; char ch = 'A'; for (int i = 0; i < colNum; i++) { dataGridView1.Columns.Add(i.ToString(), ch.ToString()); dataGridView1.Rows.Add(rowNum); for (int j = 0; j <rowNum; j++) { cellStr = ch.ToString() + (j + 1).ToString(); dataGridView1[i, j].Value = ws.UsedRange.Cells.get_Range(cellStr, miss).Text.ToString(); } ch++; } } #endregion } }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧