先谢谢昨天的各位!
昨天问了各位大侠怎么保存数据到excle中 ok了 就是加一个按钮 单击按钮然后会提示保存到那个目录
现在想加载窗体的时候就保存 不要提示保存到什么地方的对话框了 因为最终要写成一个脚步
每次运行脚步自动把数据保存到一个目录 比如d:xxx/excel.xls 这样就不用手动保存了
protected void Export_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvUsbUseUser.AllowPaging = false;//分页false
getDate();//查询数据的方法
gvUsbUseUser.RenderControl(htmlWrite);//gvUsbUseUser控件名 USB使用用户 Response.Write(stringWrite.ToString());
Response.End();
gvUsbUseUser.AllowPaging = true;//分页true
getDate();
} //确认在运行时以ASP.NET服务器控件呈现htmlFrom控件 这句不加报错 不知道为什么 重写?
public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}
昨天问了各位大侠怎么保存数据到excle中 ok了 就是加一个按钮 单击按钮然后会提示保存到那个目录
现在想加载窗体的时候就保存 不要提示保存到什么地方的对话框了 因为最终要写成一个脚步
每次运行脚步自动把数据保存到一个目录 比如d:xxx/excel.xls 这样就不用手动保存了
protected void Export_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvUsbUseUser.AllowPaging = false;//分页false
getDate();//查询数据的方法
gvUsbUseUser.RenderControl(htmlWrite);//gvUsbUseUser控件名 USB使用用户 Response.Write(stringWrite.ToString());
Response.End();
gvUsbUseUser.AllowPaging = true;//分页true
getDate();
} //确认在运行时以ASP.NET服务器控件呈现htmlFrom控件 这句不加报错 不知道为什么 重写?
public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}
if (fileInfo.Exists)
{
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
Response.WriteFile(srcPath);
Response.Flush();
Response.Close();
}
还要写个局域网发邮件的脚步 附件的路径就是d:xx.xls
没周五运行一次 这样不用登陆程序和ourlook保存数据再登陆outlook发送了
楼上可以实现自动保存吗?当然那个目录肯定不会有重复文件或者什么问题的 我等会试一下
我就是想写个脚本 每天生产一个excle到指定的目录 后台指定一个目录 有的话就覆盖 因为要的是最新的所以覆盖没事的 我在找找 望各位给个建议 谢谢
{ if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filePath)))
{ System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filePath));
}
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
try
{
app.Visible = false;
app.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook wBook = app.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.Worksheets[1];
wSheet.Name = "Sheet1";
if (excelTable.Rows.Count > 0)
{
int row = 0;
row = excelTable.Rows.Count;
int col = excelTable.Columns.Count;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
string str = excelTable.Rows[i][j].ToString();
wSheet.Cells[i + 2, j + 1] = str;
}
}
} //int size = excelTable.Columns.Count;
//for (int i = 0; i < size; i++)
//{
// wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
//}
//设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false; wBook.Save();
//保存excel文件
app.Save(filePath);
app.SaveWorkspace(filePath);
app.Quit();
wSheet = null;
wBook = null;
app = null;
GC.Collect();
return true;
}
catch (Exception err)
{
//wSheet = null;
//wBook = null;
//app = null;
app.Quit();
app = null;
GC.Collect();
return false;
}
finally
{
app = null;
GC.Collect();
}
}
那现在怎么自动保存呢 上面的代码ok吗 还需要一个excle的一个组件吧
最关键的是注释有点少 有关保存数据代码贴上啊 我先贴上我发局域带附件的代码
给需要的人 推荐啊 呵呵 static void Main(string[] args)
{ /// <summary>
/// C#发送邮件函数
/// </summary>
/// <param name="from">发送者邮箱</param>
/// <param name="fromer">发送人</param>
/// <param name="to">接受者邮箱</param>
/// <param name="toer">收件人</param>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="file">附件</param>
/// <param name="SMTPHost">smtp服务器</param>
/// <param name="SMTPuser">邮箱</param>
/// <param name="SMTPpass">密码</param>
///
string form = "[email protected]";
string former = "[email protected]";
string to = "[email protected]";
string toer = "[email protected]";
string subject = "USB权限管理统计表";
string body = "USB权限管理统计表";
string file = "D:/FileName.xls";
string SMTPHost = "10.5.13.14";
string SMTPuser = "aushelp5";
string SMTPpass = "Liang123";
accessory.sendmail(form,former,to,toer,subject,body,file,SMTPHost,SMTPuser,SMTPpass);
}以上是控制台main方法 下面是发送邮件的类 测试过了没问题 直接copy到项目中就可以了
没上传到csdn 公司上传功能被拦截了
/// <summary>
/// C#发送邮件函数
/// </summary>
/// <param name="from">发送者邮箱</param>
/// <param name="fromer">发送人</param>
/// <param name="to">接受者邮箱</param>
/// <param name="toer">收件人</param>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="file">附件</param>
/// <param name="SMTPHost">smtp服务器</param>
/// <param name="SMTPuser">邮箱</param>
/// <param name="SMTPpass">密码</param> /// <returns></returns>
public static bool sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
{
////设置from和to地址
MailAddress from = new MailAddress(sfrom, sfromer);
MailAddress to = new MailAddress(sto, stoer); ////创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to); //// 添加附件
if (sfile != "")
{
oMail.Attachments.Add(new Attachment(sfile));
} ////邮件标题
oMail.Subject = sSubject;
////邮件内容
oMail.Body = sBody; ////邮件格式
oMail.IsBodyHtml = false; ////邮件采用的编码
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312"); ////设置邮件的优先级为高
oMail.Priority = MailPriority.High; ////发送邮件
SmtpClient client = new SmtpClient();
////client.UseDefaultCredentials = false;
client.Host = sSMTPHost;
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(oMail);
return true;
}
catch (Exception err)
{
Console.WriteLine(err.Message.ToString());
return false;
}
finally
{
////释放资源
oMail.Dispose();
}
}