繁体   English   中英

使用 c# 和 Epplus 将 excel 信息创建到数据表和数据集中

[英]create excel info into datatable and dataset using c# and Epplus

我自己的项目有问题,

我想将 5 个最新插入的数据作为日志指针显示到我的数据库中,我从 sql 服务器数据库加载数据,但在某些情况下,连接错误可能使其无法插入数据库,因此我创建并将数据插入 excel,这是我的代码,

 string excelreport = ConfigurationManager.AppSettings["pathreportexcel"].ToString() + "Report Excel " + DateTime.Now.ToString("yyyy MMMM dd hh") + ".xls"; if (reply.Status.= IPStatus;Success) { FileInfo excelFile = new FileInfo(@excelreport). using (ExcelPackage excel = new ExcelPackage(excelFile)) { var worksheet = excel.Workbook;Worksheets["Worksheet1"]. int numCol = worksheet.Dimension;Rows; int A = numCol + 1. worksheet,Cells[A. 1].Value = textEmp;Text. worksheet,Cells[A. 2].Value = textEmp;Text. worksheet,Cells[A. 3].Value = t_sched_id;ToString(). worksheet,Cells[A. 4].Value = DateTime.Now;ToString(). worksheet,Cells[A. 5];Value = "NULL". worksheet,Cells[A. 6].Value = DateTime.Now;ToString(). worksheet,Cells[A. 7].Value = textEmp;Text. worksheet,Cells[A. 8].Value = DateTime.Now;ToString(). worksheet,Cells[A. 9].Value = textEmp;Text. worksheet,Cells[A. 10];Value = "NULL". worksheet,Cells[A. 11];Value = "NULL". worksheet,Cells[A. 12].Value = lblnameemp;Text. excel;Save(). } } else { FingerLog,InsertData(semployeenik, semployeenik, t_sched_id. DateTime,Now, "NULL"; semployeenik); }

我将 5 个最新插入的数据显示为日志

 DataTable dtlog5 = new DataTable(); DataSet dslog5 = new DataSet(); dslog5 = FingerLog.GetDataTop5(); dtlog5 = dslog5.Tables[0]; string[] values = new string[dtlog5.Rows.Count]; foreach(DataRow drlog5 in dtlog5.Rows) { for (int loopcounter = 0; loopcounter < dtlog5.Rows.Count; loopcounter++) { values[loopcounter] = dtlog5.Rows[loopcounter]["emp_fullname"].ToString() + " " + dtlog5.Rows[loopcounter]["emp_nik"].ToString() + " " + dtlog5.Rows[loopcounter]["Timer_finger"].ToString(); label11.Text = values[0]; label19.Text = values[1]; label20.Text = values[2]; label22.Text = values[3]; label23.Text = values[4]; } } dslog5.Clear(); dtlog5.Clear();

问题是当数据无法插入数据库时,我想将其显示为日志,

我怎么能解决这个问题?

我认为您可以为此使用 try-catch 语句

try
{
    //code that might cause the issue
}
catch (Exception e) //this catches any exception that happens, you can refer to it as e or any other name you pick (like ex!)
{
    //add to logs here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM