c# - c# winforms VS2017与excel链接:关闭表单时出错
问题描述
这被认为是重复的帖子,因为确切的代码用于另一个问题(大约 5 年前。)。基本上,我收到错误System.NullreferenceException: 'Object reference not set to an instance on an object'。xlWorkBook.Close(true, misValue, misValue);
每次我关闭表格时都换行(见图)。基于原始问题:Inserting multiple textbox data into an Excel file,我找不到与我有相同问题的其他人。我使用的代码与链接相同:
`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace Vehicledettry
{
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();
}
private void button1_Click(object sender, EventArgs e)
{
xlexcel = new Excel.Application();
xlexcel.Visible = true;
// Open a File
xlWorkBook = xlexcel.Workbooks.Open(" C:\\vehicledet.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] = "Plate Number";
xlWorkSheet.Cells[1, 2] = "Car Model";
xlWorkSheet.Cells[1, 3] = "Car Brand";
xlWorkSheet.Cells[1, 4] = "Mileage";
}
private void button2_Click(object sender, EventArgs e)
{
int _lastRow = xlWorkSheet.Cells[xlWorkSheet.Rows.Count, 1].End[Excel.XlDirection.xlUp].Row + 1;
xlWorkSheet.Cells[_lastRow, 1] = Plate Number.Text;
xlWorkSheet.Cells[_lastRow, 2] = Car Model.Text;
xlWorkSheet.Cells[_lastRow, 3] = Car Brand.Text;
xlWorkSheet.Cells[_lastRow, 4] = Mileage.Text;
}
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();
}
}
}
我尝试xlWorkBook = xlexcel.Workbooks.Open(" C:\\vehicledet.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
在每个按钮事件单击后进行复制,但同样的错误不断发生。希望我能得到一些帮助。谢谢。
解决方案
最近,我有一个项目也将数据发送到一个 excel 文件,但与你的不同,我的数据来自一个文本文件,这个网站上的某个人帮助我弄清楚如何在 Excel 文件中导出我的数组的值. 在您的情况下,此代码可能会有所帮助。
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application exc = new Excel.Application();
exc.Interactive = true;
var excelTemplate = "CompareResult.xlsx"; //Change it with your filename
string FromPath = Path.GetFullPath(excelTemplate); //Get the full path of your excel
//file.
Excel.Workbook wb = exc.Workbooks.Open(FromPath);
Excel.Worksheet sh = wb.Sheets[1];
int _lastRow = xlWorkSheet.Cells[xlWorkSheet.Rows.Count, 1].End[Excel.XlDirection.xlUp].Row + 1;
sh.Cells[row, 1].Value2 = textBox1.Text;
sh.Cells[row, 2].Value2 = textBox1.Text;
sh.Cells[row, 3].Value2 = textBox1.Text;
sh.Cells[row, 4].Value2 = textBox1.Text;
wb.Save(); \\Saving the file when changing
wb.Close();
exc.Quit();
推荐阅读
- visual-studio - 为什么 TeamCity 会同时生成 Visual Studio (.sln) 构建步骤和多个 .NET CLI (dotnet) 构建步骤?
- iis - IIS 8.5 静态压缩缺少 Content-Encoding 响应标头,但 Failed Request Tracing 显示使用 gzip 进行压缩
- c++ - 自定义 QGraphicsPixmapItem 不显示像素图
- c++ - 对这个递归函数的输出感到困惑
- dns - 如何使用 C# 中的 WMI 对象编写多字符串 (Microsoft) DNS Txt 记录
- charts - 具有多个属性的状态更新的时间序列可视化
- amazon-web-services - 如何使用一个 SSL 证书来保护多个动态 EC2 实例?
- javascript - 更漂亮的文件注释有什么用?
- c# - 使用来自绑定的内联 XAML 以程序方式生成文本
- python-3.x - MySQLDB 'utf-8' 编解码器的问题无法解码字节 0x92