EXCEL:读取多个EXCEL文件数据到WORD文档
2014-04-26 17:24
507 查看
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); 9995 } } }
相关文章推荐
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- EXCEL:读取多个WORD文档文件到EXCEL同一工作表
- java操作office和pdf文件:读取word,excel和pdf文档内容
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- java操作office和pdf文件java读取word,excel和pdf文档内容
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- asp.net从数据库导出数据到word、excel、txt文本文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- Python使用pandas读取Excel文件数据和预处理小案例
- 读取Excel文件中的数据
- 用C#编程从数据库中读取图片数据导进Excel文件的方法
- Aspose.Cells 使用整理:读取Excel文件里的数据
- robotFramework 读取Excel文件数据
- C#把DataSet内数据转化为Excel和Word文件的通用类
- word无法读取此文档,文档可能已损坏 请尝试下列方法:*打开并修复文件*用文本恢复转换器打开文件
- 操作Word模板文件.dot 结合具体数据 生成Word文档 .doc
- C#读取Excel文件数据
- ASP.NET编程插入Excel文件到word中数据区域
- C#读取Excel和txt文件数据并把数据分别一次性添加到Mysql数据库
- 结合DataGrid控件ASP.NET上传并读取Excel文件数据