Excel Sheet 不保存数据 Sheet 1

Excel Sheet Not Saving Data in Sheet 1

我 Excel 文件有 2 sheets,第一个 sheet 命名为 "PM",第二个 Sheet 命名为 "km" 当我尝试从文本框中向 sheet 1 输入数据,它已成功完成,但是当我尝试在 sheet 2 中输入数据时,sheet 1 回到其原始状态(空).. 我检查了我最初为两个 sheet 编写的代码 1 worksheet "xlsht" 但是数据没有在两个 sheet 上更新然后我用 2 个 worksheets 但问题是一样的,知道为什么吗?

我的代码:

protected void PM_Sheet1()
    {
        try
        {
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Worksheet xlsht = new Worksheet();
            string path = @"D:\test.xlsx";
            xlsht = xlApp.Application.Workbooks.Open(path).Worksheets["PM"];
            xlsht.Cells[11, 2] = UserNameTxt.Text + "@rasatop.com";
            xlsht.Cells[11, 4] = UserNameTxt.Text;
            xlsht.Cells[14, 2] = SerialTxt.Text;
            xlsht.Cells[16, 2] = WLANMacTxt.Text;
            xlsht.Cells[16, 3] = LANMacTxt.Text;
            xlsht.Cells[16, 4] = IPTxt.Text;
            xlsht.Cells[14, 5] = ComputerTxt.Text;
            xlsht.Cells[16, 5] = BarcodeTxt.Text;
            xlsht.Cells[18, 5] = CPUTxt.Text.Substring(0, 26);
            xlsht.Cells[18, 4] = VGATxt.Text;
            xlsht.Cells[18, 3] = RAMTxt.Text;
            xlsht.Cells[27, 4] = OSTxt.Text;
            xlsht.Cells[5, 4] = System.DateTime.Today;
            xlsht.Cells[26, 4] = System.DateTime.Today;
            xlsht.Cells[9, 5] = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
            xlsht.SaveAs(@"D:\test1.xlsx");
        }
        catch (Exception)
        {
            MessageBox.Show(@"Make Sure test.xlsx file in D:\ Drive");
        }
    }
    protected void PM_Sheet2()
    {
        try
        {
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Worksheet xlsht = new Worksheet();
            string path = @"D:\test1.xlsx";
            xlsht = xlApp.Application.Workbooks.Open(path).Worksheets["km"];
            xlsht.Cells[4, 2] = System.DateTime.Today;
            xlsht.Cells[6, 2] = UserNameTxt.Text;
            xlsht.Cells[6, 4] = ComputerTxt.Text;
            xlsht.Cells[6, 5] = BarcodeTxt.Text;
            xlsht.SaveAs(@"D:\" + ComputerTxt.Text + ".xlsx");
            xlApp.Visible = true;
        }
        catch (Exception)
        {
            MessageBox.Show("Error Occured Cannot Save Sheet 2");
        }

    }

尽管documentationWorksheet.SaveAs()会导致错误,所以你必须使用Workbook.SaveAs()

您还打开了两个不同的 Excel 实例,其中您只需要一个实例,显然两个实例都保留了 运行!

最后,你最好为所有操作打开一个Excel实例,因此在Form class级别(声明),button1_Click()级别( ExcelWorkbook 初始化),辅助方法(Worksheet 初始化)

如下:

using System;
using System.Windows.Forms;
using Worksheet = Microsoft.Office.Interop.Excel.Worksheet;
using Workbook = Microsoft.Office.Interop.Excel.Workbook;
using MsExcelApp = Microsoft.Office.Interop.Excel.Application;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        MsExcelApp xlApp =;
        Workbook xlWb;
        Worksheet xlSht;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"D:\test.xlsx";
            xlApp = new MsExcelApp();


            try
            {
                xlWb = xlApp.Application.Workbooks.Open(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            PM_Sheet1();
            PM_Sheet2();

            try
            {
                xlWb.SaveAs(@"D:\" + ComputerTxt.Text + ".xlsx");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            xlApp.Quit();
        }




        protected void PM_Sheet1()
        {
            try
            {
                xlSht = xlWb.Worksheets["PM"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            xlSht.Cells[11, 2] = UserNameTxt.Text + "@rasatop.com";
            xlSht.Cells[11, 4] = UserNameTxt.Text;
            xlSht.Cells[14, 2] = SerialTxt.Text;
            xlSht.Cells[16, 2] = WLANMacTxt.Text;
            xlSht.Cells[16, 3] = LANMacTxt.Text;
            xlSht.Cells[16, 4] = IPTxt.Text;
            xlSht.Cells[14, 5] = ComputerTxt.Text;
            xlSht.Cells[16, 5] = BarcodeTxt.Text;
            xlSht.Cells[18, 5] = CPUTxt.Text.Substring(0, 26);
            xlsht.Cells[18, 4] = VGATxt.Text;
            xlsht.Cells[18, 3] = RAMTxt.Text;
            xlsht.Cells[27, 4] = OSTxt.Text;
            xlSht.Cells[5, 4] = System.DateTime.Today;
            xlSht.Cells[26, 4] = System.DateTime.Today;
            xlSht.Cells[9, 5] = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
        }

        protected void PM_Sheet2()
        {
           try
            {
                xlSht = xlWb.Worksheets["km"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            xlSht.Cells[4, 2] = System.DateTime.Today;
            xlSht.Cells[6, 2] = UserNameTxt.Text;
            xlSht.Cells[6, 4] = ComputerTxt.Text;
            xlSht.Cells[6, 5] = BarcodeTxt.Text;
        }
    }


}