添加项目文件。
This commit is contained in:
159
口罩泄露定制款/Excle/UserExcleHelper.cs
Normal file
159
口罩泄露定制款/Excle/UserExcleHelper.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using OfficeOpenXml;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace 口罩泄露定制款.Excle
|
||||
{
|
||||
internal class UserExcleHelper
|
||||
{ //文件路径
|
||||
static string path = Application.StartupPath + @"\UserExcleHelper.xlsx";
|
||||
//向表格中写入用户数据
|
||||
public void WriteData(string username, string password, string quanxian)
|
||||
{
|
||||
bool isHave = false;
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
//创建Excel对象
|
||||
ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(path));
|
||||
// 获取第一个工作表,如果不存在则创建
|
||||
ExcelWorksheet worksheet;
|
||||
if (package.Workbook.Worksheets.Count == 0)
|
||||
{
|
||||
worksheet = package.Workbook.Worksheets.Add("用户表");
|
||||
worksheet.Cells[1, 1].Value = "用户名";
|
||||
worksheet.Cells[1, 2].Value = "密码";
|
||||
worksheet.Cells[1, 3].Value = "用户权限";
|
||||
worksheet.Cells[2, 1].Value = "admin";
|
||||
worksheet.Cells[2, 2].Value = "admin123456";
|
||||
worksheet.Cells[2, 3].Value = "管理员";
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet = package.Workbook.Worksheets[0];
|
||||
}
|
||||
//判断用户名是否存在
|
||||
for (int i = 2; i <= worksheet.Dimension.Rows; i++)
|
||||
{
|
||||
if (worksheet.Cells[i, 1].Value.ToString() == username)
|
||||
{
|
||||
isHave = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isHave)
|
||||
{
|
||||
MessageBox.Show("用户名已存在!");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
int row = worksheet.Dimension.Rows + 1;
|
||||
worksheet.Cells[row, 1].Value = username;
|
||||
worksheet.Cells[row, 2].Value = password;
|
||||
worksheet.Cells[row, 3].Value = quanxian;
|
||||
//保存并关闭文件
|
||||
package.Save();
|
||||
}
|
||||
|
||||
}
|
||||
//读取表格用户名数据
|
||||
public List<string> ReadUserNameData()
|
||||
{
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
//创建Excel对象
|
||||
ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(path));
|
||||
// 获取第一个工作表,如果不存在则创建
|
||||
ExcelWorksheet worksheet;
|
||||
if (package.Workbook.Worksheets.Count == 0)
|
||||
{
|
||||
// WriteData("","","");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet = package.Workbook.Worksheets[0];
|
||||
}
|
||||
List<string> list = new List<string>();
|
||||
for (int i = 2; i <= worksheet.Dimension.Rows; i++)
|
||||
{
|
||||
list.Add(worksheet.Cells[i, 1].Value.ToString());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
//读取所有单元格信息并存放在datatable中
|
||||
public DataTable ReadUserData()
|
||||
{
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
//创建Excel对象
|
||||
ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(path));
|
||||
// 获取第一个工作表,如果不存在则创建
|
||||
ExcelWorksheet worksheet;
|
||||
if (package.Workbook.Worksheets.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet = package.Workbook.Worksheets[0];
|
||||
}
|
||||
DataTable dt = new DataTable();
|
||||
dt.Columns.Add("用户名");
|
||||
dt.Columns.Add("密码");
|
||||
dt.Columns.Add("用户权限");
|
||||
for (int i = 2; i <= worksheet.Dimension.Rows; i++)
|
||||
{
|
||||
DataRow dr = dt.NewRow();
|
||||
dr["用户名"] = worksheet.Cells[i, 1].Value.ToString();
|
||||
dr["密码"] = worksheet.Cells[i, 2].Value.ToString();
|
||||
dr["用户权限"] = worksheet.Cells[i, 3].Value.ToString();
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
//修改用户信息
|
||||
public void UpdateUserData(int index, string username, string password, string quanxian)
|
||||
{
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
//创建Excel对象
|
||||
ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(path));
|
||||
// 获取第一个工作表,如果不存在则创建
|
||||
ExcelWorksheet worksheet;
|
||||
if (package.Workbook.Worksheets.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet = package.Workbook.Worksheets[0];
|
||||
}
|
||||
|
||||
worksheet.Cells[index, 1].Value = username;
|
||||
worksheet.Cells[index, 2].Value = password;
|
||||
worksheet.Cells[index, 3].Value = quanxian;
|
||||
|
||||
//保存并关闭文件
|
||||
package.Save();
|
||||
}
|
||||
//删除用户信息
|
||||
public void DeleteUserData(int index)
|
||||
{
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
//创建Excel对象
|
||||
ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(path));
|
||||
// 获取第一个工作表,如果不存在则创建
|
||||
ExcelWorksheet worksheet;
|
||||
if (package.Workbook.Worksheets.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet = package.Workbook.Worksheets[0];
|
||||
}
|
||||
//删除指定行
|
||||
worksheet.DeleteRow(index);
|
||||
//保存并关闭文件
|
||||
package.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user