在excel中添加新行,c#winforms应用程序 [英] adding new row in excel ,c# winforms application
本文介绍了在excel中添加新行,c#winforms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要打开现有的Excel工作表,并添加一个新行,其中包含从文本框收集的数据。我正在使用以下代码。但通过使用此文件是相关的。文件扩展名为:xlsx
Hi,
i need to open existing excel sheet, and add a new row with data collecting from textboxes. am using following code. but by using this the file is correpted. file extension is:xlsx
<pre lang="c#"></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 System.IO;
using Excel=Microsoft.Office.Interop.Excel;
namespace Hand_Held_Data_Transporter
{
public partial class AddAccount : Form
{
private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
private static Microsoft.Office.Interop.Excel.Application oXL;
object Missing = System.Reflection.Missing.Value;
public AddAccount()
{
InitializeComponent();
}
private void btnsave_Click(object sender, EventArgs e)
{
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = true;
oXL.DisplayAlerts = false;
//error on this line
mWorkBook = oXL.Workbooks.Open(txtbrowse.Text, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Get all the sheets in the workbook
mWorkSheets = mWorkBook.Worksheets;
//Get the allready exists sheet
mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
int colCount = range.Columns.Count;
int rowCount = range.Rows.Count;
for (int index = 1; index < 15; index++)
{
mWSheet1.Cells[rowCount + index, 1] = rowCount + index;
mWSheet1.Cells[rowCount + index, 2] = "New Item" + index;
}
mWorkBook.SaveAs(txtbrowse.Text, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
Missing, Missing, Missing, Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Missing, Missing, Missing,
Missing, Missing);
mWorkBook.Close(Missing, Missing, Missing);
mWSheet1 = null;
mWorkBook = null;
oXL.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Filter = "All Files(*.*)|*.*";
if (fdlg.ShowDialog() == DialogResult.OK)
{
txtbrowse.Text = fdlg.FileName;
File.ReadAllText(txtbrowse.Text);
}
}
}
}
>
推荐答案
Please have a look on the bellow link :
How to open an excel file in C# windows form[^]
How to append existing excel file using C# [^]
How to open an Excel fil e in C#?[^]
Please have a look on the bellow link :
How to open an excel file in C# windows form[^]
How to append existing excel file using C# [^]
How to open an Excel file in C#?[^]
这篇关于在excel中添加新行,c#winforms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文