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;
}
}
}
- poi导出word表格跨行
DataCommon.java package com.ksource.pwlp.model.statistic; public class DataCommon { private Long id; ...
- poi导出word表格详解 超详细了
转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138 一.效果如下 二.js代码 function export_word( ...
- PowerDesiger 15逆向生成工程E-R图及导出word表格
应用环境:win8(64位)+oracle10g(32位)服务端+PowerDesigner15 需求:oracle数据库中的表结构是web工程框架hibernate 自动生成,现需要将数据库中已有的 ...
- c#(.net) 导出 word表格
做了差不多一周的导出Word,现在把代码贴出来 : ExportWord.cs using System; using System.Collections.Generic; using Syst ...
- poi导出word表格
代码如下: package com.ksource.pwlp.util; import java.io.FileOutputStream; import java.math.BigInteger; i ...
- .net使用AsposeWord导出word table表格
本文为原创,转载请注明出处 1.前言 .net平台下导出word文件还可以使用Microsoft.Office.Interop和NPOI,但是这两者都有缺点,微软的Office.Interop组件需要 ...
- Java使用velocity导出word
效果展示: 使用word编辑好模板
- Freemarker + xml 实现Java导出word
前言 最近做了一个调查问卷导出的功能,需求是将维护的题目,答案,导出成word,参考了几种方案之后,选择功能强大的freemarker+固定格式之后的wordxml实现导出功能.导出word的代码是可 ...
- 使用NPOI2.1.3.1版本导出word附带表格和图片
原文:http://www.cnblogs.com/afutureBoss/p/4074397.html?utm_source=tuicool&utm_medium=referral 最近项目 ...
随机推荐
- 获取webconfig配置文件内容
string ServerUrl= ConfigurationManager.AppSettings["ServerUrl"].ToString(); web.config中的配置 ...
- LeetCode--寻找数组中心索引
给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和. 如果数组不存在中心索引,那么我 ...
- CentOS7搭建KMS服务器
使用vlmcsd搭建KMS服务器 1.下载vlmcsd: wget https://github.com/Wind4/vlmcsd/releases/download/svn1111/binaries ...
- webpack打包出错,通过babel将es6转es5的出错。
错误信息如下: 解决方法: 1,先安装babel-preset-es2015到项目中, cnpm install babel-preset-es2015 --save-dev2,在项目根目录中新建一个 ...
- 爬虫文件存储:txt文档,json文件,csv文件
5.1 文件存储 文件存储形式可以是多种多样的,比如可以保存成 TXT 纯文本形式,也可以保存为 Json 格式.CSV 格式等,本节我们来了解下文本文件的存储方式. 5.1.1 TXT文本存储 将数 ...
- 二叉堆 及 大根堆的python实现
Python 二叉堆(binary heap) 二叉堆是一种特殊的堆,二叉堆是完全二叉树或者是近似完全二叉树.二叉堆满足堆特性:父节点的键值总是保持固定的序关系于任何一个子节点的键值,且每个节点的左子 ...
- flask中的session cookie 测试 和 项目中的用户状态保持
# -*- coding:utf-8 -*- # Author: json_steve from flask import Flask, current_app, make_response, req ...
- java后台处理解析json字符串的两种方式
简单说一下背景 上次后端通过模拟http请求百度地图接口,得到的是一个json字符串,而我只需要其中的某个key对应的value. 当时我是通过截取字符串取的,后来觉得不太合理,今天整理出了两种处理解 ...
- Android音乐、视频类APP常用控件:DraggablePanel(2)
Android音乐.视频类APP常用控件:DraggablePanel(2) 附录文章1主要演示了如何使用DraggablePanel 的DraggableView.DraggablePanel ...
- RSAROLL
题目:http://www.shiyanbar.com/ctf/1918 # -*- coding: utf-8 -*- import gmpy2 ciper = [704796792, 752211 ...