gpt4 book ai didi

c# - 将多个文本框数据插入到 Excel 文件中

转载 作者:太空狗 更新时间:2023-10-30 01:20:22 25 4
gpt4 key购买 nike

我想编写一个程序,使用循环将文本框中的文本保存到 Excel 文件,因为我想将多个文本插入到 Excel 中。我找到了代码,但它只会覆盖单元格中的数据。我希望程序找到最后一行并将新数据插入下一行。我被困在这里,请有人帮助我如何在 C# 中做到这一点。

object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.Cells[1, 1] = "FirstName";
xlWorkSheet.Cells[1, 2] = "LastName";
xlWorkSheet.Cells[1, 3] = "JobTitle";
xlWorkSheet.Cells[1, 4] = "Address";

for (int i=2; i<=6; i++)
{
xlWorkSheet.Cells[i, 1] = textBox1.Text;
xlWorkSheet.Cells[i, 2] = textBox2.Text;
xlWorkSheet.Cells[i, 3] = textBox3.Text;
xlWorkSheet.Cells[i, 4] = textBox4.Text;
}

最佳答案

正如我提到的,您不需要使用循环。看这个例子

假设您的表单如下所示。

enter image description here

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

Namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

Public Form1()
{
InitializeComponent();
}

//~~> Open File
private void button1_Click(object sender, EventArgs e)
{
xlexcel = new Excel.Application();

xlexcel.Visible = true;

// Open a File
xlWorkBook = xlexcel.Workbooks.Open("C:\\MyFile.xlsx", 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.Cells[1, 1] = "FirstName";
xlWorkSheet.Cells[1, 2] = "LastName";
xlWorkSheet.Cells[1, 3] = "JobTitle";
xlWorkSheet.Cells[1, 4] = "Address";
}

//~~> Add Data
private void button2_Click(object sender, EventArgs e)
{
int _lastRow = xlWorkSheet.Range["A" + xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1 ;

xlWorkSheet.Cells[_lastRow, 1] = textBox1.Text;
xlWorkSheet.Cells[_lastRow, 2] = textBox2.Text;
xlWorkSheet.Cells[_lastRow, 3] = textBox3.Text;
xlWorkSheet.Cells[_lastRow, 4] = textBox4.Text;
}

//~~> Once done close and quit Excel
private void button3_Click(object sender, EventArgs e)
{
xlWorkBook.Close(true, misValue, misValue);
xlexcel.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlexcel);
}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}

评论的跟进

范围对象是工作表对象的一部分。所以你不应该在那里得到任何错误。就像我上面提到的,代码已经过测试。

enter image description here

更多跟进(来自评论)

以上代码是在 VS 2010 Ultimate 上测试的。如果您有 VS 2008,则替换该行

int _lastRow = xlWorkSheet.Cells[xlWorkSheet.Rows.Count,
1].End[Excel.XlDirection.xlUp].Row + 1;

int _lastRow = xlWorkSheet.Cells.Find(
"*",
xlWorkSheet.Cells[1,1],
Excel.XlFindLookIn.xlFormulas,
Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlPrevious,
misValue,
misValue,
misValue
).Row + 1 ;

关于c# - 将多个文本框数据插入到 Excel 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19353980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com