62,266
社区成员
发帖
与我相关
我的任务
分享
protected void LinkButton1_Click(object sender, EventArgs e)
{
//try
//{
string id = Session["caseinfoId"].ToString();
//从数据库中取值
string sql = @"select a.dsrname as dsr1,a.xb as xb_1,a.sfcode as code1,a.hjdz as hjdz1,a.jzdz as xjd1,
b.dsrname as dsr2,b.xb as xb_2,b.sfcode as code2,b.hjdz as hjdz2,b.jzdz as xjd2,caseinfo.jbr as jbr,
caseinfo.jbrdz as jbrdz,caseinfo.jbrtel as jbrtel,caseinfo.aqms as aqjj,caseinfo.cbnbyj as nbyj,caseinfo.ajly as ajly,caseinfo.ajqtly as ajqtly,
caseinfo.cbry as cbr,caseinfo.Lashyj as shyj,caseinfo.Lashfzr as bmfzr,caseinfo.Jaspyj as spyj,caseinfo.Jaspfzr as jgfzr,caseinfo.Lanf as nf,caseinfo.Labh as bh
from (select * from casedsr where dsrbh=1 ) as a inner join caseinfo on caseinfo.id=a.ajbh inner join (select * from casedsr where dsrbh=2 )as b on caseinfo.id=b.ajbh where caseinfo.id=" + id;
DataSet ds = sqlHelper.ExecuteDataset(sqlHelper.ConnectionStringLocalTransaction, sql);
DataRow dr = ds.Tables[0].Rows[0];
string path = Server.MapPath("~/doc/ladj.doc");
string outpath = Server.MapPath("~/doc/ls/s.doc");
GetDocByTemplet(path, outpath, dr, true);
System.Threading.Thread.Sleep(100); //给系统足够的时间,让doc能顺利转换调用它的线程
downLoad("doc/ls/s.doc");
//}
//catch
//{
//对word导出失败的提示
//RegisterStartupScript("", "<Script Language=JavaScript>alert('word导出失败!')</Script>");
//}
}
private void GetDocByTemplet(string TempletPathName, string OutPathName, DataRow Row, bool IgnoreBookMarkMissing) //word处理函数 string OutPathName,
{
#region
if (!File.Exists(TempletPathName))
throw new Exception("模板文件不存在");
//tsPath.checkDirectory(tsPath.ExtractFilePath(OutPathName));
//复制模板文件覆盖目标文件
if (File.Exists(OutPathName))
{
File.Delete(OutPathName);
}
File.Copy(TempletPathName, OutPathName, true);
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
try
{
object oOutPath = TempletPathName;
oDoc = oWord.Documents.Add(ref oOutPath, ref oMissing, ref oMissing, ref oMissing); //将模板作为一对象进行修改
foreach (Microsoft.Office.Interop.Word.Bookmark bookMark in oDoc.Bookmarks)
{
if (Row.Table.Columns.IndexOf(bookMark.Name) >= 0)
{
if (bookMark.Name.Split(new char[] { '_' })[0] == "xb")
{
if (Row[bookMark.Name].ToString().Trim() == "1") // 性别的判断
{
bookMark.Range.Text = "男";
}
else
{
bookMark.Range.Text = "女";
}
}
else
{
bookMark.Range.Text = Row[bookMark.Name].ToString().Trim();
}
}
else if (bookMark.Name.Split(new char[] { '_' })[0] == "ajly" && bookMark.Name.Split(new char[] { '_' })[1] == Row["ajly"].ToString())
{
bookMark.Range.Text = "√";
}
else if (!IgnoreBookMarkMissing)
{
throw new Exception("不存在的数据:" + bookMark.Name);
}
}
//将修改结果保存在用户选择的路径下
//oDoc.Save();
object p = OutPathName;
//将修改的结果另存为
oDoc.SaveAs(ref p, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
finally
{
object o = false;
oWord.Application.Quit(ref o, ref oMissing, ref oMissing);
}
#endregion
}
private void downLoad(string loadPath)
{
string FullFileName = loadPath;
FileInfo DownloadFile = new FileInfo(HostingEnvironment.ApplicationPhysicalPath + FullFileName); // 需要转换为绝对路径,否则会自动认到C盘系统里那个IIS目录下面去,而且,无法通过URI的方式来进行数据流读取。如果你生成的文件不在web目录下,也需要明确指出。
// 下面到就是读取文件,通过数据流的方式下载了。
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}