[置顶] 数据导出Excel

时间:2022-05-04 14:48:28

 public static void WriteEmailSendStatsExcelFile(string mapPath, string fileName, IList<EmailContent> list)
        {
            lock (myLock)
            {
                try
                {
                    DeleteSheetData(mapPath, fileName);
                    Connection(mapPath + fileName);
                    objConnection.Open();
                    string sendStatus = "";

                    //////修改:过长字段导致导出不能错误 2010-4-26
                    objCommand = new OleDbCommand();
                    string strCreateTable = "create table [Sheet1$] ([邮箱地址] Memo , [邮件类型] Memo , [客户姓名] Memo , [渠道] Memo , [机构] Memo , [发送时间] Memo ,[发送状态] Memo , [内容] Memo , [备注] Memo )";

                    objCommand.CommandText = strCreateTable;
                    objCommand.Connection = objConnection;
                    objCommand.ExecuteNonQuery();
                    /////

                    foreach (EmailContent line in list)
                    {
                        if (line.SendStatus.Equals((int)SendStatus.UnSend))
                            sendStatus = "未发送";
                        else if (line.SendStatus.Equals((int)SendStatus.SendFailure))
                        {
                            if (line.Remark != null && line.Remark != "")
                            {
                                sendStatus = line.Remark;
                            }
                            else
                                sendStatus = "发送失败";
                        }
                        else if (line.SendStatus.Equals((int)SendStatus.Sended))
                            sendStatus = "发送成功";
                        else if (line.SendStatus.Equals((int)SendStatus.Cancel))
                            sendStatus = "取消发送";
                        objCommand.CommandText = "insert into [Sheet1$] (邮箱地址,邮件类型,客户姓名,渠道,机构,发送时间,发送状态,内容,备注) values(@EmailAddress,@TypeName,@ClientName,@Channel,@OrgName,@RealSendTime,@sendStatus,@Message,@Remark)";

                        objCommand.Parameters.Add("EmailAddress", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("TypeName", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("ClientName", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("Channel", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("OrgName", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("RealSendTime", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("sendStatus", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("Message", OleDbType.LongVarChar);
                        objCommand.Parameters.Add("Remark", OleDbType.LongVarChar);
                        objCommand.Parameters[0].Value = line.EmailAddress;
                        objCommand.Parameters[1].Value = line.TypeName == null ? "" : line.TypeName;
                        objCommand.Parameters[2].Value = line.ClientName == null ? "" : line.ClientName;
                        objCommand.Parameters[3].Value = line.Channel == null ? "" : line.Channel;
                        objCommand.Parameters[4].Value = line.OrgName == null ? "" : line.OrgName;
                        objCommand.Parameters[5].Value = line.RealSendTime == null ? "" : line.RealSendTime.ToString();
                        objCommand.Parameters[6].Value = sendStatus == null ? "" : sendStatus;
                        objCommand.Parameters[7].Value = line.Message == null ? "" : line.Message;
                        objCommand.Parameters[8].Value = line.Remark == null ? "" : line.Remark;
                        objCommand.ExecuteNonQuery();
                    }
                }
                finally
                {
                    if (objCommand != null)
                        objCommand.Dispose();
                    if (objConnection != null && objConnection.State == ConnectionState.Open)
                        objConnection.Close();
                }
            }
        }