高手给看看 谢谢!!!
我要在GV里把数据导到Excel里,但是导出来的Excel修改数据在导回数据库的时候数据库没有修改后的数据。。。。protected void Page_Load(object sender, EventArgs e)
{
BindGridData();
}
private void BindGridData()
{
gvSuccessManage.DataSource = nbo.dao.FindAll();
gvSuccessManage.DataBind();
}
private void Export(string FileType, string FileName)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
gvSuccessManage.AllowPaging = false;
gvSuccessManage.AllowSorting = false;
BindGridData();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gvSuccessManage.RenderControl(hw);
Response.Write(style);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void btnImport_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
}
protected void gvSuccessManage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Attributes.Add("class", "text");
e.Row.Cells[3].Attributes.Add("class", "text");
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName != "")
{
//上传文件的绝对路径
string sFile = FileUpload1.PostedFile.FileName;
//获取文件全名
sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
//获取后缀名
sFile = sFile.Substring(sFile.LastIndexOf("."));
string dbName = Path.GetFileName(FileUpload1.FileName).Replace(".xls", "");
if (sFile.ToLower() != ".xls")
{
Response.Write("请选择Excel文件!");
Response.End();
}
//为了防止重名,获得日期为文件名年月日时分秒毫秒
string datatime = System.DateTime.Now.ToString("yyyMMddHHmmssffff");
//上传后文件的新名
sFile = datatime + sFile;
AppDomain.CurrentDomain.BaseDirectory.ToString(); //获取此项目的根目录
//sPath 获取上传后的路径
var directory = AppDomain.CurrentDomain.BaseDirectory.ToString() + "ExcelFiles\\";
string sPath = directory + sFile;
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
//上传文件
FileUpload1.PostedFile.SaveAs(sPath);
var ds = nbo.GetExcelContent(sPath, dbName);
var dt = ds.Tables[0];
int count = 0;
NRSVO vo = new NRSVO();
foreach (DataRow dr in dt.Rows)
{
if (count != 0)
{
vo.username = dr[0].ToString();
vo.age = Convert.ToInt32(dr[1].ToString());
vo.birthday = Convert.ToDateTime(dr[2].ToString());
vo.identityID = dr[3].ToString();
vo. unit = dr[4].ToString();
vo. phone = dr[5].ToString();
vo. address = dr[6].ToString();
vo. grade = Convert.ToInt32(dr[7].ToString());
nbo.Save(vo);
}
count++;
}
}
BindGridData();
}
}