none
操作Excel问题! RRS feed

  • 问题

  • public partial class Form1 : Form
        {
           
    private string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "\\Book.xls" + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
           
    private OleDbConnection mycoon;
           
    private Microsoft.Office.Interop.Excel.Application app;
           
    private Workbook book;
           
    private Worksheet sheet;

           
    public Form1()
            {
                InitializeComponent();
                mycoon
    = new OleDbConnection(strConn);
                app
    = new Microsoft.Office.Interop.Excel.ApplicationClass();
                app.Visible
    = false;
            }

           
    private void Form1_Load(object sender, EventArgs e)
            {
               
    try
                {
                    mycoon.Open();
                    book
    = app.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\Book.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value
        , Missing.Value, Missing.Value, Missing.Value, Missing.Value
        , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    sheet
    =(Worksheet) book.Sheets[1];
                }
               
    catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,
    "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   
    this.Close();
                   
    return;
                }
            }

           
    private void button1_Click(object sender, EventArgs e)
            {
                DataSet myds_sheet1
    = new DataSet();
               
    string sql_sheet1 = "SELECT * FROM  [Sheet1$]";
                OleDbCommand mycmd_sheet1
    = new OleDbCommand(sql_sheet1, mycoon);
                OleDbDataAdapter myda_sheet1
    = new OleDbDataAdapter(mycmd_sheet1);
               
    try
                {
                    myda_sheet1.Fill(myds_sheet1);
                }
               
    catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,
    "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   
    return;
                }
               
    for (int i = 0; i < myds_sheet1.Tables[0].Rows.Count; i++)
                {
                   
    if (myds_sheet1.Tables[0].Rows[i][2].ToString() == "")
                       
    break;
                   
    for (int j = 0; i < myds_sheet1.Tables[0].Rows.Count; j++)
                    {
                       
    if (myds_sheet1.Tables[0].Rows[i][2].ToString().Trim() == myds_sheet1.Tables[0].Rows[j][0].ToString().Trim())
                        {
                           
    //((Range)sheet.Cells[i, 2]).Value2 = (j + 1).ToString();
                            MessageBox.Show(((Range)sheet.Cells[i, 1]).Value2.ToString());
                        }
                    }
                }
                book.Save();
                sheet
    = null;
                book
    = null;
                app.Quit();
                app
    = null;
            }
        }


    当执行到MessageBox.Show(((Range)sheet.Cells[i, 1]).Value2.ToString());时,会出现一个错误异常来自 HRESULT:0x800A03EC

    请高手帮忙解决


    Excel中的第一列和三列有数据,第二列为空列。

    2009年9月12日 8:56

答案

  • 你好!
         可以参考这个帖子,希望对你有帮助:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/3725e365-b740-4d5a-8f53-820f38d92513
    周雪峰
    2009年9月12日 9:58
    版主