using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class PrintWord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// QueryDataSet();
}
/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnPrint_Click(object sender, EventArgs e)
{
Print();
}
/// <summary>
/// 导出Word
///生成word解决方案
///1.先引入模板
///2.判断是否和查询数据集一样的行,如果小于数据集就要增到行数
///3. 获取表格并且并且为每一个单元格赋值
///4.根据不同的条件来设置单元格的颜色变化
/// </summary>
private void Print()
{
//先引入word
Microsoft.Office.Interop.Word.Application app; //明一个应用
Microsoft.Office.Interop.Word.Document doc; //创建一个文档
string TemplateFile = ""; // 声明要使用的模板名称
string FileName = ""; // 新文件的路径名称
string Fname = ""; //新文件名称
app = new Microsoft.Office.Interop.Word.Application();//创建实例应用
doc = new Microsoft.Office.Interop.Word.Document(); //创建实例文档
TemplateFile = Server.MapPath("~/test/半月带预测-长安汽车每日快报2015010x.dot"); //Server.MapPath("~/test/CAXSMB.dot");//找到模板
Fname = DateTime.Now.ToString("测试相同的文档名试试") + ".doc";//创建新文件的名称 yyyymmddhhmmss
FileName = Server.MapPath("~/test/download/" + Fname);//新文件的路径
//判断有相同的文档就要删除
if (File.Exists(FileName))
{
File.Delete(FileName);
}
File.Copy(TemplateFile, FileName);//把模板拷贝到新文件
//为新文件设置属性
object Obj_FileName = FileName;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
//创建新文档
doc = app.Documents.Open(
ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref Visible,
ref missing, ref missing, ref missing,
ref missing);
//增加表格
//DataSet ds = GetDataSet();//获取程序集合
//int dsCount = ds.Tables[0].Rows.Count;//得到数据库表中的数据
//int docCount = doc.Tables[1].Rows.Count; //得到文档中表格的数据
//if (dsCount > docCount) //当实际数据大约表格数据的时候创建行
//{
// //开始增加行
// Microsoft.Office.Interop.Word.Table newTable = doc.Tables[1];
// for (int row = 0; row < dsCount - docCount; row++)
// {
// object beforeRow = doc.Tables[1].Rows[docCount-1];
// doc.Tables[1].Rows.Add(ref beforeRow); // 行添加到表格中
// }
//}
doc.Activate();
// 匹配表格数据集
Microsoft.Office.Interop.Word.Document odoc = GetDocument(FileName);//获取新生的文档
DataSet ds = GetDataSet();//获取程序集合
//开始判断里面有几个表格/因为模板里面有三张表
for (int tablePos = 1; tablePos <= odoc.Tables.Count; tablePos++)
{
//都一张表的时候
if (tablePos == 1)
{
int dsCount = ds.Tables[0].Rows.Count;//得到数据库表中的数据
int docCount = odoc.Tables[1].Rows.Count; //得到文档中表格的数据
if (dsCount > docCount) //当实际数据大约表格数据的时候创建行
{
//开始增加行
Microsoft.Office.Interop.Word.Table newTable = doc.Tables[1];
for (int row = 0; row < (dsCount - docCount)+1; row++)
{
object beforeRow = newTable.Rows[docCount];
newTable.Rows.Add(ref beforeRow); // 行添加到表格中
}
}
//表格完成之后开始增加数据,为单元格赋值
for (int rcount = 0; rcount < ds.Tables[0].Rows.Count; rcount++)
{
for (int ccount = 0; ccount < ds.Tables[0].Columns.Count; ccount++)
{
//doc.Tables[1].Rows[rowPos].Cells[columPos].Range.Text
string strText = ds.Tables[0].Rows[rcount][ccount].ToString();
doc.Tables[1].Rows[rcount+2].Cells[ccount + 1].Range.Text = strText;//从第二行开始算起
if (strText == "r")
{
doc.Tables[1].Rows[rcount+2].Cells[ccount + 1].Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorLightBlue;
}
}
}
}
}
//关闭进程
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);
app.Quit(ref IsSave, ref missing, ref missing); //关闭word进程
string url = "~/test/download/" + Fname; ;
Response.Redirect(url);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app); //释放内存空间
}
/// <summary>
/// 查询数据集
/// </summary>
/// <returns></returns>
public DataSet GetDataSet()
{
string sConnectionString = "server=.;uid=sa;pwd=123456;database=xiaoshoudb";
SqlConnection objConn = new SqlConnection(sConnectionString);
objConn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from yuexiaoshou", objConn);
DataSet ds = new DataSet();
da.Fill(ds);
objConn.Close();
return ds;
}
/// <summary>
/// 根据文件地址得到该文件下属性
/// </summary>
/// <param name="fileRoad"></param>
/// <returns></returns>
public Microsoft.Office.Interop.Word.Document GetDocument(string fileRoad)
{
Microsoft.Office.Interop.Word.Application app;
app = new Microsoft.Office.Interop.Word.Application();
object oFileName = fileRoad; //Server.MapPath(fileRoad);//根据word的路径 //("~/test/测试读写.docx"); // @"F:\数据库.docx";
object oReadOnly = false;
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();
oWord.Visible = false;
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
return oDoc;
}
}
}