做作业的时候查了一点儿资料, 用的vs2k5 读 excel 2007 发现用起来非常简单。。。现在编程语言没话说! 1 项目-添加引用-COM-Microsoft Excel 12.0 Object Library && -Microsoft Office 12.0 Object Library 2 using Microsoft.Office.Core; using Microsoft.Office.Interop.Excel; using System.IO; using System.Reflection; 3 string originalFile = System.Windows.Forms.Application.StartupPath + @".a.xlsx"; string outputFile; SaveFileDialog save = new SaveFileDialog(); save.InitialDirectory = "D:\"; save.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; save.FilterIndex = 1; save.RestoreDirectory = true; if (save.ShowDialog() == DialogResult.OK) { try { outputFile = save.FileName; System.IO.File.Copy(originalFile, outputFile, true); ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass(); //打开目标文件outputFile RSbook = ExcelRS.Workbooks.Open(outputFile, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //设置第一个工作溥 RSsheet = (Microsoft.Office.Interop.Excel.Worksheet)RSbook.Sheets.get_Item(1); //激活当前工作溥 RSsheet.Activate(); RSsheet.Cells[1, 1] = dataGridView1.SelectedRows.Count; for (int i = 0; i < dataGridView1.SelectedRows.Count; ++i) { for (int j = 0; j < 11; ++j) { RSsheet.Cells[i + 2, j + 1] = dataGridView1.Rows[dataGridView1.SelectedRows[i].Index].Cells[j].Value.ToString().Trim(); // RSsheet.Cells RSsheet.get_Range(RSsheet.Cells[i + 2, j + 1], RSsheet.Cells[i + 2, j + 1]).EntireColumn.ColumnWidth = 40; //RSsheet.get_Range(RSsheet.Cells[i + 2, j + 1], missing).auto } } RSbook.Save(); ExcelRS.DisplayAlerts = true; ExcelRS.Visible = true; } catch (System.Exception ex) { MessageBox.Show(ex.Message); } finally { ExcelRS.Quit(); } } 4 OpenFileDialog open = new OpenFileDialog(); open.InitialDirectory = @"D:"; open.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; open.FilterIndex = 1; open.RestoreDirectory = true; if (open.ShowDialog() == DialogResult.OK) { try { ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass(); RSbook = ExcelRS.Workbooks.Open(open.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); RSsheet = (Microsoft.Office.Interop.Excel.Worksheet)RSbook.Sheets.get_Item(1); RSsheet.Activate(); Microsoft.Office.Interop.Excel.Range range = RSsheet.get_Range("A" + i, Type.Missing); counts = int.Parse(range.Text.ToString().Trim()); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } finally { ExcelRS.Quit(); conn.Close(); } } 评论这张 转发至微博 转发至微博 阅读(675)| 评论(0) | 用微信 “扫一扫” 将文章分享到朋友圈。 用易信 “扫一扫” 将文章分享到朋友圈。 喜欢 推荐 0人 | 转载