假如我們需要在一個word文檔的某個位置插入一張表格,而且要對這一張表格中的單元格做拆分合並,之類的。
先看下效果,先不管表格是否合理,總之就是要這樣的統計文檔,但是人數,班級數都是不確定的,也就是表格是根 據數據動態生成的,
這樣我們就很難用之前的替換方式來實現,也就是要動態來創建表格。
班級成績統計單
| 班級 | 姓名 | 成績 | 人數總計 | 班主任 |
| 一班 | 張三 | 498 | 1 | 小李 |
| 二班 | 李四 | 354 | 2 | 陳飛 |
| 小紅 | 502 | |||
| 三班 | 丁爽 | 566 | 1 | 王林 |
1、用單元格拆分的方式實現,也就是根據有多少班級,則增加多少行,針對於其中的學生信息再對單元格進行拆分。
/// <summary> /// 數據實體類 /// </summary> public class Student { public string Name;//姓名 public int Score;//成績 public string StuClass;//班級 public string Leader;//班主任 } /// <summary> /// 動態創建table到word /// </summary> protected void CreateTableToExcel() { Word.Application app = null; Word.Document doc = null; try { //構造數據 List<Student> datas = new List<Student>(); datas.Add(new Student{ Leader="小李", Name="張三", Score=498, StuClass="一班"}); datas.Add(new Student{ Leader="陳飛", Name="李四", Score=354, StuClass="二班"}); datas.Add(new Student{ Leader="陳飛", Name="小紅", Score=502, StuClass="二班"}); datas.Add(new Student{ Leader="王林", Name="丁爽", Score=566, StuClass="三班"}); var cate = datas.GroupBy(s=>s.StuClass); int rows = cate.Count()+1;//表格行數加1是為了標題欄 int cols = 5;//表格列數 object oMissing = System.Reflection.Missing.Value; app = new Word.Application();//創建word應用程序 doc = app.Documents.Add();//添加一個word文檔 //輸出大標題加粗加大字號水平居中 app.Selection.Font.Bold = 700; app.Selection.Font.Size = 16; app.Selection.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; app.Selection.Text = "班級成績統計單"; //換行添加表格 object line = Word.WdUnits.wdLine; app.Selection.MoveDown(ref line, oMissing, oMissing); app.Selection.TypeParagraph();//換行 Word.Range range = app.Selection.Range; Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing); //設置表格的字體大小粗細 table.Range.Font.Size = 10; table.Range.Font.Bold=0; //設置表格標題 int rowIndex = 1; table.Cell(rowIndex, 1).Range.Text = "班級"; table.Cell(rowIndex, 2).Range.Text = "姓名"; table.Cell(rowIndex, 3).Range.Text = "成績"; table.Cell(rowIndex, 4).Range.Text = "人數"; table.Cell(rowIndex, 5).Range.Text = "班主任"; //循環數據創建數據行 rowIndex++; foreach (var i in cate) { table.Cell(rowIndex, 1).Range.Text = i.Key;//班級 table.Cell(rowIndex, 4).Range.Text = i.Count().ToString();//人數 table.Cell(rowIndex, 5).Range.Text = i.First().Leader;//班主任 table.Cell(rowIndex,2).Split(i.Count(), 1);//分割名字單元格 table.Cell(rowIndex,3).Split(i.Count(), 1);//分割成績單元格 //對表格中的班級、姓名,成績單元格設置上下居中 table.Cell(rowIndex, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; table.Cell(rowIndex, 4).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; table.Cell(rowIndex, 5).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; //構建姓名,成績數據 foreach (var x in i) { table.Cell(rowIndex, 2).Range.Text = x.Name; table.Cell(rowIndex, 3).Range.Text = x.Score.ToString(); rowIndex++; } } //導出到文件 string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc"; string physicNewFile = Server.MapPath(newFile); doc.SaveAs(physicNewFile, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); } catch(Exception ex) { } finally { if (doc != null) { doc.Close();//關閉文檔 } if (app != null) { app.Quit();//退出應用程序 } } }
2、單元格合並的方式,即循環數據輸出,然后對班級,人數,班主任列進行合並,但是合並的時候需要注意,假如某班級只有一個同學,也就是其實不需要合並的,
則必須排除在外,否則會出現”此命令無效“的異常,所以下面代碼在合並的時候做了一個判斷。
protected void CreateTableToExcel2() { Word.Application app = null; Word.Document doc = null; try { //構造數據 List<Student> datas = new List<Student>(); datas.Add(new Student { Leader = "小李", Name = "張三", Score = 498, StuClass = "一班" }); datas.Add(new Student { Leader = "陳飛", Name = "李四", Score = 354, StuClass = "二班" }); datas.Add(new Student { Leader = "陳飛", Name = "小紅", Score = 502, StuClass = "二班" }); datas.Add(new Student { Leader = "王林", Name = "丁爽", Score = 566, StuClass = "三班" }); var cate = datas.GroupBy(s => s.StuClass); int rows = datas.Count + 1; int cols = 5;//表格列數 object oMissing = System.Reflection.Missing.Value; app = new Word.Application();//創建word應用程序 doc = app.Documents.Add();//添加一個word文檔 //輸出大標題加粗加大字號水平居中 app.Selection.Font.Bold = 700; app.Selection.Font.Size = 16; app.Selection.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; app.Selection.Text = "班級成績統計單"; //換行添加表格 object line = Word.WdUnits.wdLine; app.Selection.MoveDown(ref line, oMissing, oMissing); app.Selection.TypeParagraph();//換行 Word.Range range = app.Selection.Range; Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing); //設置表格的字體大小粗細 table.Range.Font.Size = 10; table.Range.Font.Bold = 0; //設置表格標題 int rowIndex = 1; table.Cell(rowIndex, 1).Range.Text = "班級"; table.Cell(rowIndex, 2).Range.Text = "姓名"; table.Cell(rowIndex, 3).Range.Text = "成績"; table.Cell(rowIndex, 4).Range.Text = "人數"; table.Cell(rowIndex, 5).Range.Text = "班主任"; //循環數據創建數據行 rowIndex++; foreach (var i in cate) { int moveCount = i.Count() - 1;//縱向合並行數 if (moveCount.ToString() != "0") { table.Cell(rowIndex, 1).Merge(table.Cell(rowIndex + moveCount, 1));//合並班級 table.Cell(rowIndex, 4).Merge(table.Cell(rowIndex + moveCount, 4));//合並人數 table.Cell(rowIndex, 5).Merge(table.Cell(rowIndex + moveCount, 5));//合並班主任 } //寫入合並的數據並垂直居中 table.Cell(rowIndex, 1).Range.Text = i.Key; table.Cell(rowIndex, 4).Range.Text = i.Count().ToString(); table.Cell(rowIndex, 5).Range.Text = i.First().Leader; table.Cell(rowIndex, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; table.Cell(rowIndex, 4).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; table.Cell(rowIndex, 5).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; //構建姓名,成績數據 foreach (var x in i) { table.Cell(rowIndex, 2).Range.Text = x.Name; table.Cell(rowIndex, 3).Range.Text = x.Score.ToString(); rowIndex++; } } //導出到文件 string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc"; string physicNewFile = Server.MapPath(newFile); doc.SaveAs(physicNewFile, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); } catch (Exception ex) { } finally { if (doc != null) { doc.Close();//關閉文檔 } if (app != null) { app.Quit();//退出應用程序 } } }
