如何将TextBox值输入(从UserForm)保存到工作表上的单元格?

时间:2016-06-07 13:29:15

标签: c# excel

enter image description here

我的表格看起来像这样。我的目标是将13个文本框/组合框添加到Excel工作表中。此外,我在这下面有2个按钮,这些按钮没有显示在名为' Save'和'添加' - 保存非常自我解释。添加是为了保存来自UserForm的输入并将其放入具有相应标题的Excel列中,清除UserForm,最后,准备用户再次输入将进入下面一行的数据。到目前为止,这是我在加法按钮中的代码:

public partial class Form1 : Form
{
    Microsoft.Office.Interop.Excel.Application oXL;
    Microsoft.Office.Interop.Excel._Workbook oWB;
    Microsoft.Office.Interop.Excel._Worksheet oSheet;
    Microsoft.Office.Interop.Excel.Range oRng;
    int num;

    public Form1()
    {
        oXL = new Microsoft.Office.Interop.Excel.Application();
        oXL.Visible = true;

        oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
        oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        num++;

        oSheet.Cells[1, 1] = "UserName";
        oSheet.Cells[1, 2] = "Workstation Name";
        oSheet.Cells[1, 3] = "Manufacturer";
        oSheet.Cells[1, 4] = "Model";
        oSheet.Cells[1, 5] = "Serial";
        oSheet.Cells[1, 6] = "CPU";
        oSheet.Cells[1, 7] = "RAM";
        oSheet.Cells[1, 8] = "OS";
        oSheet.Cells[1, 9] = "Version";
        oSheet.Cells[1, 10] = "Microsoft Office";
        oSheet.Cells[1, 11] = "Recommendations";
        oSheet.Cells[1, 12] = "Comments";

        oSheet.get_Range("A1", "L1").Font.Bold = true;
        oSheet.get_Range("A1", "L1").VerticalAlignment =
        Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;

        string[,] saNames = new string[100, 13];

        saNames[num, 0] = txtUsername1.Text;
        saNames[num, 1] = txtWorkName1.Text;
        saNames[num, 2] = cbxManufac.Text;
        saNames[num, 3] = cbxMachType.Text;
        saNames[num, 4] = txtModel.Text;
        saNames[num, 5] = txtSerial.Text;
        saNames[num, 6] = txtCPU.Text;
        saNames[num, 7] = cbxRAM.Text;
        saNames[num, 8] = cbxOS.Text;
        saNames[num, 9] = txtVersion.Text;
        saNames[num, 10] = txtMcstOffice.Text;
        saNames[num, 11] = txtRecomend.Text;
        saNames[num, 12] = txtComments.Text;

        oSheet.get_Range("A2", "L1000").Value = saNames;
    }

我遇到的问题是我的添加按钮没有将输入保存到excel单元格中,它消失了,我不确定我做错了所以我决定问那些可能遇到过这个的人之前的问题。

此外,我似乎从第34行开始; A3"而不是" A2"有点奇怪

2 个答案:

答案 0 :(得分:1)

我不是这种excel杂技的专家,但你可以试试这个

public partial class Form1 : Form
{
    Microsoft.Office.Interop.Excel.Application oXL;
    Microsoft.Office.Interop.Excel._Workbook oWB;
    Microsoft.Office.Interop.Excel._Worksheet oSheet;
    Microsoft.Office.Interop.Excel.Range oRng;
    int num = 1;

    public Form1()
    {
        oXL = new Microsoft.Office.Interop.Excel.Application();
        oXL.Visible = true;

        oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
        oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // here you create the Headers once the Form is loaded
        initHeaders();
    }

    // a Method to create the Headers in the file
    private void initHeaders()
    {
        oSheet.Cells[1, 1] = "UserName";
        oSheet.Cells[1, 2] = "Workstation Name";
        oSheet.Cells[1, 3] = "Manufacturer";
        oSheet.Cells[1, 4] = "Model";
        oSheet.Cells[1, 5] = "Serial";
        oSheet.Cells[1, 6] = "CPU";
        oSheet.Cells[1, 7] = "RAM";
        oSheet.Cells[1, 8] = "OS";
        oSheet.Cells[1, 9] = "Version";
        oSheet.Cells[1, 10] = "Microsoft Office";
        oSheet.Cells[1, 11] = "Recommendations";
        oSheet.Cells[1, 12] = "Comments";

        oSheet.get_Range("A1", "L1").Font.Bold = true;
        oSheet.get_Range("A1", "L1").VerticalAlignment =
        Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;

    }
    private void button2_Click(object sender, EventArgs e)
    {
        num++;

        // you actually need only a one dimensional array    
        string[] saNames = new string[13];

        saNames[0] = txtUsername1.Text;
        saNames[1] = txtWorkName1.Text;
        saNames[2] = cbxManufac.Text;
        saNames[3] = cbxMachType.Text;
        saNames[4] = txtModel.Text;
        saNames[5] = txtSerial.Text;
        saNames[6] = txtCPU.Text;
        saNames[7] = cbxRAM.Text;
        saNames[8] = cbxOS.Text;
        saNames[9] = txtVersion.Text;
        saNames[10] = txtMcstOffice.Text;
        saNames[11] = txtRecomend.Text;
        saNames[12] = txtComments.Text;

        // Try to increment just the position in the file
        string startposition = "A" + num.toString();
        oSheet.get_Range(startposition , "L1000").Value = saNames;
    }
}

答案 1 :(得分:1)

private void button2_Click(object sender, EventArgs e)    
{
    int _lastRow = oSheet.Range["A" + oSheet.Rows.Count].End[Microsoft.Office.Interop.Excel.XlDirection.xlUp].Row + 1;

    oSheet.Cells[_lastRow, 1] = txtUsername1.Text;
    oSheet.Cells[_lastRow, 2] = txtWorkName1.Text;
    oSheet.Cells[_lastRow, 3] = cbxManufac.Text;
    oSheet.Cells[_lastRow, 4] = cbxMachType.Text;
    oSheet.Cells[_lastRow, 5] = txtModel.Text;
    oSheet.Cells[_lastRow, 6] = txtSerial.Text;
    oSheet.Cells[_lastRow, 7] = txtCPU.Text;
    oSheet.Cells[_lastRow, 8] = cbxRAM.Text;
    oSheet.Cells[_lastRow, 9] = cbxOS.Text;
    oSheet.Cells[_lastRow, 10] = txtVersion.Text;
    oSheet.Cells[_lastRow, 11] = txtMcstOffice.Text;
    oSheet.Cells[_lastRow, 12] = txtRecomend.Text;
    oSheet.Cells[_lastRow, 13] = txtComments.Text;

}

我修好了:o