using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using Microsoft.Office.Interop.Excel;//关键
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Microsoft.Office.Interop.Excel.Application excelApplication = null;
private Workbooks excelWorkBooks = null;
private Workbook excelWorkBook = null;
private Worksheet excelWorkSheet = null;
private Range excelRange = null;//Excel Range Object,多种用途
public int ActiveSheetIndex = 0;
private void Form1_Load(object sender, EventArgs e)
{
excelApplication = null;//Excel Application Object
excelWorkBooks = null;//Workbooks
excelWorkBook = null;//Excel Workbook Object
excelWorkSheet = null;//Excel Worksheet Object
ActiveSheetIndex = 1;
//测试以下的函数是否正确
OpenExcelFile("F:\\ee.xls");
//setOneCellValue(2, 2, "i love u");
//MessageBox.Show("cell:"+getOneCellValue(2,2));
//MessageBox.Show("value:" + getCellsValue(excelWorkSheet.Cells[1,2], excelWorkSheet.Cells[2,3])[0]);
setCellsValue(excelWorkSheet.Cells[2, 2], excelWorkSheet.Cells[3, 3], "same");
SetActiveWorkSheet(1);
System.Data.DataTable dt = new System.Data.DataTable();
dt = getAllCellsValue();
dataGridView1.DataSource = dt;
dt.TableName = "table1";
dt.AcceptChanges();
//MessageBox.Show("value:" + getTotalRowCount().ToString());
//MessageBox.Show("value:" + getTotalColumnCount().ToString());
saveExcel("F:\\save2.xls");
}
public void saveExcel(string excelSaveFileName)
{
if (File.Exists(excelSaveFileName))
{
if (MessageBox.Show(excelSaveFileName + "已存在,需要覆盖吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
File.Delete(excelSaveFileName);
excelWorkSheet.SaveAs(excelSaveFileName, Excel.XlFileFormat.xlTemplate, Type.Missin