C# 保存文件时发生EPPlus对象引用错误。为什么会这样?
我正在创建一个基本程序,将一些数据写入excel。我是C#的新手,当然对EPPlus也是新手。我很难找到关于这个软件包的好文档,但是 我的代码如下C# 保存文件时发生EPPlus对象引用错误。为什么会这样?,c#,epplus,C#,Epplus,我正在创建一个基本程序,将一些数据写入excel。我是C#的新手,当然对EPPlus也是新手。我很难找到关于这个软件包的好文档,但是 我的代码如下 using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Thre
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OfficeOpenXml;
using Microsoft.VisualBasic;
/* CHANGELOG
* 11/26/2019
* Added basic support for excel database using EPPlus library
*
*/
/* BUGS, FIXES, AND TODO
*
* ******BUGS******
*
* [] Fix loop to loop through each item in given array instead of specified length. Perhaps a foreach makes more sense?
*
*
*
* ******TODO******
* [x] Figure out best way to write information to a file (Excel?)
* [] Create Duplicate Entry detector?
* [] Possible to loop through args in visitor constructor?
* [] Prefill information from membership info (Bar code on mem card?)
* [] Set tab index of all items
* [] Allow user to export database to excel w/ path of choosing.
* []
*
*
*/
namespace Nonprofit_Attendance_App
{
public partial class Form1 : Form
{
//Declare Global Variables
public static class Globals
{
public static string excelSheet = "";
public static ExcelWorksheet wsSheet1;
public static ExcelPackage ExcelPkg;
public static string userIn;
public static string[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
public static int dataPosition;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Create new visitor object with form input
Visitor Paal = new Visitor( vis_zip.Text,
vis_entrance_courtYard.Checked,
vis_entrance_frontDoor.Checked,
vis_reason_general.Checked,
vis_reason_meeting.Checked,
vis_reason_photography.Checked,
vis_reason_cafe.Checked,
vis_firstTime.Checked,
vis_returning.Checked,
vis_both.Checked,
vis_mem.Checked,
vis_mem_lastName.Text );
//Loop through Visitor object to write data to console. DELETE EVENTUALLY
foreach (var item in Paal.vis_data)
{
Console.WriteLine(item);
}
writeData(Paal.vis_data);
//Clear form after submit
vis_zip.Text = string.Empty;
vis_entrance_courtYard.Checked = false;
vis_entrance_frontDoor.Checked = false;
vis_reason_general.Checked = false;
vis_reason_meeting.Checked = false;
vis_reason_photography.Checked = false;
vis_reason_cafe.Checked = false;
vis_firstTime.Checked = false;
vis_returning.Checked = false;
vis_both.Checked = false;
vis_mem.Checked = false;
vis_mem_lastName.Text = string.Empty;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
class Visitor
{
public string[] vis_data = new string[12];
public Visitor(string zipCode, bool entrance_courtYard, bool entrance_frontDoor, bool vis_reason_general, bool vis_reason_meeting, bool vis_reason_photo, bool vis_reason_cafe, bool vis_firstTime, bool vis_Returning, bool vis_both, bool vis_mem, string vis_mem_lastName)
{
//Write to Excel Sheet
//FIX EMPTY ENTRIES
vis_data[0] = zipCode;
if (entrance_courtYard == true)
{
vis_data[1] = "Courtyard";
}
else
{
vis_data[1] = "Front Door";
}
if (vis_reason_general == true)
{
vis_data[2] += "General Visit";
}
else if (vis_reason_meeting == true)
{
vis_data[2] += "Meeting";
}
else if (vis_reason_cafe == true)
{
vis_data[2] += "Cafe";
}
else if (vis_reason_photo == true)
{
vis_data[2] += "Photography";
}
else
{
vis_data[2] = "null";
}
if (vis_firstTime == true)
{
vis_data[3] = "First Time";
}
else if (vis_Returning == true)
{
vis_data[3] = "Returning";
}
else if (vis_both == true)
{
vis_data[3] = "Both";
}
else
{
vis_data[3] = "null";
}
if (vis_mem)
{
vis_data[4] = vis_mem_lastName;
}
else
{
vis_data[4] = "Not a member";
}
vis_data[5] = DateTime.Now.ToString();
}
}
private void vis_frontDoor_CheckedChanged(object sender, EventArgs e)
{
}
//Enable member last name when member checkbox is checked
private void vis_mem_CheckedChanged(object sender, EventArgs e)
{
if (vis_mem.Checked)
{
vis_mem_lastName.Enabled = true;
}
else
{
vis_mem_lastName.Enabled = false;
}
}
private void vis_mem_lastName_TextChanged(object sender, EventArgs e)
{
}
//Generates Excel database
public static void generateExcel()
{
//get user input for database name
//Excel Sheet Generation
Globals.ExcelPkg = new ExcelPackage();
Globals.wsSheet1 = Globals.ExcelPkg.Workbook.Worksheets.Add("Sheet1");
Globals.wsSheet1.Protection.IsProtected = false;
Globals.wsSheet1.Protection.AllowSelectLockedCells = false;
Globals.excelSheet = @"C:\Users\paalw\documents\database.xlsx";
Globals.ExcelPkg.SaveAs(new FileInfo(@"C:\Users\paalw\documents\database.xlsx"));
}
//TO FINISH
public static void setHeaders()
{
using (ExcelRange Rng = Globals.wsSheet1.Cells)
{
for (int i = 0; i < 12; i++)
{
Rng[Globals.alphabet[i] + 1].Value = i;
}
}
}
public void getDataPosition()
{
using (ExcelRange Rng = Globals.wsSheet1.Cells)
{
for (int i = 0; i < 5; i++)
{
if (Convert.ToBoolean(Rng["A" + i].Value) == false)
{
Globals.dataPosition = i;
Console.WriteLine(Globals.dataPosition);
break;
}
}
}
}
public void writeData(string[] x)
{
using (ExcelRange Rng = Globals.wsSheet1.Cells)
{
int i = 0;
int j = Globals.dataPosition;
foreach (var item in x)
{
Rng[Globals.alphabet[i] + j].Value = x[i];
i++;
}
Globals.dataPosition++;
}
}
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (File.Exists(Globals.excelSheet))
{
Globals.excelSheet = @"C:\Users\paalw\documents\database.xlsx";
//NEED TO set FileLocation to existing Sheet
MessageBox.Show("Connected to Databse");
getDataPosition();
}
else
{
generateExcel();
setHeaders();
getDataPosition();
}
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Globals.ExcelPkg.Save();
}
}
}
使用System.IO;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用OfficeOpenXml;
使用Microsoft.VisualBasic;
/*变更日志
* 11/26/2019
*使用EPPlus库添加了对excel数据库的基本支持
*
*/
/*错误、修复和待办事项
*
********臭虫******
*
*[]修复给定数组中每个项的循环,而不是指定长度。也许一个foreach更有意义?
*
*
*
*******待办事项******
*[x]找出将信息写入文件的最佳方式(Excel?)
*[]是否创建重复输入检测器?
*[]是否可以在访问者构造函数中循环使用参数?
*[]会员信息中的预填充信息(mem卡上的条形码?)
*[]设置所有项目的选项卡索引
*[]允许用户通过选择的路径将数据库导出到excel。
* []
*
*
*/
名称空间非营利组织\u出席\u应用程序
{
公共部分类Form1:Form
{
//声明全局变量
公共静态类全局
{
公共静态字符串excelSheet=“”;
公共静态工作表wsSheet1;
公共静态包ExcelPkg;
公共静态字符串userIn;
公共静态字符串[]字母={“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”、“K”、“L”、“M”、“N”、“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X”、“Y”、“Z”};
公共静态位置;
}
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有void radioButton2\u CheckedChanged(对象发送方,事件参数e)
{
}
私有无效复选框1\u CheckedChanged(对象发送方,事件参数e)
{
}
私有无效复选框4\u CheckedChanged(对象发送方,事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//使用表单输入创建新的访问者对象
访客Paal=新访客(vis_zip.Text,
查看入口和庭院,
检查前门入口,
检查一般原因,
查看原因会议。已检查,
检查原因,
vis_reason_cafe.已检查,
第一次检查,
vis_.Checked,
都检查过了,
vis_mem.已检查,
vis_mem_lastName.Text);
//循环访问访问者对象以将数据写入控制台。最终删除
foreach(Paal.vis_数据中的var项目)
{
控制台写入线(项目);
}
书面数据(Paal.vis_数据);
//提交后清除表格
vis_zip.Text=string.Empty;
vis_入口_庭院。选中=假;
vis_entry_frontDoor.Checked=假;
vis_reason_general.Checked=false;
vis_reason_meeting.Checked=假;
vis_reason_photography.Checked=假;
vis_reason_cafe.Checked=假;
vis_firstTime.Checked=false;
vis_returning.Checked=false;
vis_both.Checked=false;
vis_mem.Checked=false;
vis_mem_lastName.Text=string.Empty;
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
}
私有无效标签6_单击(对象发送方,事件参数e)
{
}
班级访客
{
公共字符串[]可视数据=新字符串[12];
公众访客(字符串zipCode、布尔入口\庭院、布尔入口\前门、布尔可视原因\概述、布尔可视原因\会议、布尔可视原因\照片、布尔可视原因\咖啡馆、布尔可视原因\首次、布尔可视原因\返回、布尔可视原因\二者、布尔可视原因\成员、字符串可视原因\姓氏)
{
//写入Excel工作表
//修复空条目
可视数据[0]=zipCode;
if(入口\庭院==真)
{
vis_数据[1]=“庭院”;
}
其他的
{
可视数据[1]=“前门”;
}
如果(vis_reason_general==真)
{
vis_数据[2]+=“一般访问”;
}
else if(vis_reason_meeting==真)
{
可视数据[2]+=“会议”;
}
否则如果(vis_reason_cafe==真)
{
vis_数据[2]+=“Cafe”;
}
否则,如果(vis_reason_photo==真)
{
视觉数据[2]+=“摄影”;
}
其他的
{
可视数据[2]=“空”;
}
if(vis_firstTime==true)
{
可视数据[3]=“第一次”;
}
else if(vis_Returning==true)
{
可视数据[3]=“返回”;
}
else if(vis_all==true)
{
可视数据[3]=“两者”;
}
其他的
{
可视数据[3]=“空”;
}
如果(对成员)
{
vis_data[4]=vis_mem_lastName;
}
其他的
{
可视数据[4]=
public static void GenerateExcel(List<Users> users, string _path)
{
using (ExcelPackage exPackage = new ExcelPackage())
{
ExcelWorksheet ws = exPackage.Workbook.Worksheets.Add(Path.GetFileNameWithoutExtension(_path));
ws.Cells.Style.Font.SetFromFont(new Font("Calibri", 10));
ws.Cells.AutoFitColumns();
ExcelRange aHeaderRange = ws.Cells["A1:E1"];
aHeaderRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
aHeaderRange.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
short rNo = 1;
ws.Cells[rNo, 1].Value = "Customer No";
ws.Cells[rNo, 2].Value = "Name";
ws.Cells[rNo, 3].Value = "DoB";
ws.Cells[rNo, 4].Value = "Designation";
ws.Cells[rNo, 5].Value = "Address";
rNo = 2;
foreach (var m in users)
{
ws.Cells[rNo, 1].Value = m.CustNo;
ws.Cells[rNo, 2].Value = m.Name;
ws.Cells[rNo, 3].Value = m.DoB;
ws.Cells[rNo, 4].Value = m.Designation;
ws.Cells[rNo, 5].Value = m.Address;
rNo++;
}
FileInfo excelFile = new FileInfo(_path);
exPackage.SaveAs(excelFile);
}
}
public class Users
{
public string CustNo { get; set; }
public string Name { get; set; }
public string DoB { get; set; }
public string Designation { get; set; }
public string Address { get; set; }
}