EXCEL:读取多个EXCEL文件数据到WORD文档
来源:互联网 发布:百度淘宝秒杀插件 编辑:程序博客网 时间:2024/05/01 06:37
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Word = Microsoft.Office.Interop.Word;using System.Threading;using office = Microsoft.Office.Core;using System.Reflection;using System.IO;using System.Text.RegularExpressions;using System.Text;using System.Data;using System.Data.OleDb;public partial class _Default : System.Web.UI.Page { private object P_obj_WordName//记录Word文件路径及名称 { get { return ViewState["P_obj_WordName"]; } set { ViewState["P_obj_WordName"] = value ; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindListBox(); txtSavePath.Text = Server.MapPath("~");//初始化文件保存路径 } } protected void btnRead_Click(object sender, EventArgs e) { object missing = System.Reflection.Missing.Value;//获取缺少的object类型值 string[] P_str_Names = new string[ListBox1.Items.Count];//存储所有选择的Excel文件名 for (int i = 0; i < ListBox1.Items.Count; i++) { P_str_Names[i] = ListBox1.Items[i].Value; } object P_obj_Name;//存储遍历到的Excel文件名 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();//实例化Word对象 if (txtSavePath.Text.EndsWith("\\"))//判断路径是否以\结尾 P_obj_WordName = txtSavePath.Text + DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc";//记录Word文件路径及名称 else P_obj_WordName = txtSavePath.Text + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc";//记录Word文件路径及名称 Microsoft.Office.Interop.Word.Table table;//声明Word表格对象 Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();//声明Word文档对象 document = word.Documents.Add(ref missing, ref missing, ref missing, ref missing);//新建Word文档 Microsoft.Office.Interop.Word.Range range;//声明范围对象 int P_int_Rows = 0, P_int_Columns = 0;//存储工作表中数据的行数和列数 object P_obj_start = 0, P_obj_end = 0;//分别记录创建表格的开始和结束范围 object P_obj_Range = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;//定义要合并的范围位置 for (int i = 0; i < P_str_Names.Length; i++)//遍历所有选择的Excel文件名 { P_obj_Name = P_str_Names[i];//记录遍历到的Excel文件名 List<string> P_list_SheetName = CBoxBind(P_obj_Name.ToString());//获取指定Excel中的所有工作表 for (int j = 0; j < P_list_SheetName.Count; j++)//遍历所有工作表 { range = document.Range(ref missing, ref missing);//获取Word范围 range.InsertAfter(P_obj_Name + "——" + P_list_SheetName[j] + "工作表");//插入文本 range.Font.Name = "宋体";//设置字体 range.Font.Size = 10;//设置字体大小 DataSet myds = CBoxShowCount(P_obj_Name.ToString(), P_list_SheetName[j]);//获取工作表中的所有数据 P_int_Rows = myds.Tables[0].Rows.Count;//记录工作表的行数 P_int_Columns = myds.Tables[0].Columns.Count;//记录工作表的列数 range.Collapse(ref P_obj_Range);//合并范围 if (P_int_Rows > 0 && P_int_Columns > 0)//判断如果工作表中有记录 { //在指定范围处添加一个指定行数和列数的表格 table = range.Tables.Add(range, P_int_Rows, P_int_Columns, ref missing, ref missing); for (int r = 0; r < P_int_Rows; r++)//遍历行 { for (int c = 0; c < P_int_Columns; c++)//遍历列 { table.Cell(r + 1, c + 1).Range.InsertAfter(myds.Tables[0].Rows[r][c].ToString());//将遍历到的数据添加到Word表格中 } } } object P_obj_Format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;//定义Word文档的保存格式 word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;//设置保存时不显示对话框 //保存Word文档 object P_obj_WordName1 = this.P_obj_WordName; document.SaveAs(ref P_obj_WordName1, ref P_obj_Format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); this.P_obj_WordName = P_obj_WordName1; } } document.Close(ref missing, ref missing, ref missing);//关闭Word文档 word.Quit(ref missing, ref missing, ref missing);//退出Word应用程序 Response.Write("<script>alert('已经成功将多个Excel文件的内容读取到了一个Word文档中!');</script>"); } //获取指定Excel中的所有工作表 private List<string> CBoxBind(string P_str_Excel) { List<string> P_list_SheetName = new List<string>();//实例化泛型集合对象 //连接Excel数据库 OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + P_str_Excel + ";Extended Properties=Excel 8.0"); olecon.Open();//打开数据库连接 System.Data.DataTable DTable = olecon.GetSchema("Tables");//实例化表对象 DataTableReader DTReader = new DataTableReader(DTable);//实例化表读取对象 while (DTReader.Read())//循环读取 { string P_str_Name = DTReader["Table_Name"].ToString().Replace('$', ' ').Trim();//记录工作表名称 if (!P_list_SheetName.Contains(P_str_Name))//判断泛型集合中是否已经存在该工作表名称 { P_list_SheetName.Add(P_str_Name);//将工作表名添加到泛型集合中 } } DTable = null;//清空表对象 DTReader = null;//清空表读取对象 olecon.Close();//关闭数据库连接 return P_list_SheetName;//返回泛型集合 } //获取Excel工作表中的数据 private DataSet CBoxShowCount(string P_str_Excel, string P_str_Name) { //连接Excel数据库 OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + P_str_Excel + ";Extended Properties=Excel 8.0"); OleDbDataAdapter oledbda = new OleDbDataAdapter("select * from [" + P_str_Name + "$]", olecon);//从工作表中查询数据 DataSet myds = new DataSet();//实例化数据集对象 oledbda.Fill(myds);//填充数据集 return myds;//返回数据集 } protected void btnBrowse_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start(P_obj_WordName.ToString());//打开生成的Word文件 } private void BindListBox() { ListBox1.Items.Clear(); string path = Server.MapPath("~/File"); string[] files = System.IO.Directory.GetFiles(path); foreach (string f in files) { string fileName = System.IO.Path.GetFileName(f); ListItem li = new ListItem(fileName,f); ListBox1.Items.Add(li); } }}
0 0
- EXCEL:读取多个EXCEL文件数据到WORD文档
- EXCEL:读取多个WORD文档文件到EXCEL同一工作表
- 读取word excel 文件
- 读取word excel 文件
- 读取多个EXCEL文件
- springboot + poi word文档导出,Excel文件读取
- 读取Excel文件,加载数据到DataGridView
- 多个excel文档合并到一个excel文档里
- 利用POI读取Excel数据,实现同时读取多个Excel文件,兼容所有浏览器
- 将DataGrid数据导出到Excel和Word文档
- 数据 导出到Word 、excel
- 导出数据到Excel/Word
- 数据输出到WORD文件或EXCEL文件的路
- java poi读取word、excel文档
- POI 创建,读取excel。处理word文档
- 使用NOPI读取Word、Excel文档内容
- 把多个excel文件的sheet1数据合并到一个excel文件的sheet1中
- Excel vba从excel中导出表格数据和图表到新word文档
- 做图像处理的应该了解的
- HDU 2516 取石子游戏
- 移植Qt-4.8.5出现undefined reference to '__sync_sub_and_fetch_4'解决方法
- Android 核心分析 之六 -----IPC框架分析 Binder,Service,Service manager
- tolua++ vs2013 lua5.1
- EXCEL:读取多个EXCEL文件数据到WORD文档
- 编程之美 -- 寻找发帖水王,以及扩展题
- 异步懒人加载图片(GET和POST请求方式)
- 【libthread.so.0】undefined reference to symbol 'pthread_setspecific@@GLIBC_2.2.5'
- 大S首度曝光女儿正面照:小小幸福分享给大家
- Mysql 命令行导入sql数据,访问网站出现中文乱码的解决
- 黑马程序员_高新技术_JDK1.5的新特性
- 接口(Interface)的应用方法
- consistent hash