public static Word.Column GetTableColumn(Word.Table table, int i) { #if OFFICE2003 return(table.Columns[i]); #else return(table.Columns.Item(i)); #endif }
public static Word.Border GetCellBorder(Word.Table table, int row, int col, Word.WdBorderType borderType) { #if OFFICE2003 return(table.Cell(row, col).Borders[borderType]); #else return(table.Cell(row, col).Borders.Item(borderType)); #endif }
public void ExportData(DataGridView srcDgv, string fileName) //导出数据,传入一个datagridview和一个文件路径 { string type = fileName.Substring(fileName.IndexOf(".") + 1); //获得数据类型 if (type.Equals("xls", StringComparison.CurrentCultureIgnoreCase)) //Excel文档 { SaveAs(); } //保存Word文件 if (type.Equals("doc", StringComparison.CurrentCultureIgnoreCase)) { object path = fileName; Object none = System.Reflection.Missing.Value; Word.Application wordApp = new Word.Application(); Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none); //建立表格 Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count + 1, srcDgv.Columns.Count, ref none, ref none); try { for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题 { table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText; } for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据 { for (int j = 0; j < srcDgv.Columns.Count; j++) { table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString(); } } document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none); } finally { wordApp.Quit(ref none, ref none, ref none); } } }
public void CreateTalbe(DataSet ObjData, string strBookMarkName) { if (ObjData.Tables[0].Rows.Count != 0) { object missing = System.Reflection.Missing.Value; //object start = 0; //object end = 0; //Word.Range tableLocation = oWordApplic.ActiveDocument.Range(ref start, ref end); Word.Range tableLocation = GotoBookMark(strBookMarkName); int rowCount; int colCount; ////判断是否包含选择列 //bool hasSelectColumn; //hasSelectColumn = false; //foreach (TableCell tc in HeadRow.Cells) //{ // if (tc.Text == " ") // { // hasSelectColumn = true; // break; // } //} //if (hasSelectColumn) //{ // colCount = HeadRow.Cells.Count - 1; //} //else //{ // colCount = HeadRow.Cells.Count; //} //在WORD中生成表格,列数为表的列数,行数为表的行数加1,因为有列标题行 ObjData.Tables[0].Columns.Remove("project_code"); ObjData.Tables[0].Columns.Remove("contract_name"); ObjData.Tables[0].Columns.Remove("num"); DataTable dt = ObjData.Tables[0]; colCount = dt.Columns.Count; rowCount = dt.Rows.Count + 1; Word.Table objTable = oWordApplic.Selection.Tables.Add(tableLocation, rowCount, colCount, ref missing, ref missing); Word.Border border = objTable.Borders[Word.WdBorderType.wdBorderLeft]; border.LineStyle = Word.WdLineStyle.wdLineStyleSingle; border.LineWidth = Word.WdLineWidth.wdLineWidth050pt; border.Color = Word.WdColor.wdColorAutomatic; border = objTable.Borders[Word.WdBorderType.wdBorderRight]; border.LineStyle = Word.WdLineStyle.wdLineStyleSingle; border.LineWidth = Word.WdLineWidth.wdLineWidth050pt; border.Color = Word.WdColor.wdColorAutomatic; border = objTable.Borders[Word.WdBorderType.wdBorderTop]; border.LineStyle = Word.WdLineStyle.wdLineStyleSingle; border.LineWidth = Word.WdLineWidth.wdLineWidth050pt; border.Color = Word.WdColor.wdColorAutomatic; border = objTable.Borders[Word.WdBorderType.wdBorderBottom]; border.LineStyle = Word.WdLineStyle.wdLineStyleSingle; border.LineWidth = Word.WdLineWidth.wdLineWidth050pt; border.Color = Word.WdColor.wdColorAutomatic; border = objTable.Borders[Word.WdBorderType.wdBorderHorizontal]; border.LineStyle = Word.WdLineStyle.wdLineStyleSingle; border.LineWidth = Word.WdLineWidth.wdLineWidth050pt; border.Color = Word.WdColor.wdColorAutomatic; border = objTable.Borders[Word.WdBorderType.wdBorderVertical]; border.LineStyle = Word.WdLineStyle.wdLineStyleSingle; border.LineWidth = Word.WdLineWidth.wdLineWidth050pt; border.Color = Word.WdColor.wdColorAutomatic; int colIndex; int rowIndex; //创建列标题行 colIndex = 0; if (ObjData.Tables[0].TableName == "project_contract_estate_element") { foreach (DataColumn dc in dt.Columns) { colIndex += 1; if (dc.ColumnName == "obligee") { objTable.Cell(1, colIndex).Range.InsertAfter("房地产权利人"); } else if (dc.ColumnName == "credentials") { objTable.Cell(1, colIndex).Range.InsertAfter("房地产证号"); } else if (dc.ColumnName == "detail") { objTable.Cell(1, colIndex).Range.InsertAfter("房地产名称、栋号、房号"); } } } else if (ObjData.Tables[0].TableName == "project_contract_chattel_element") { foreach (DataColumn dc in dt.Columns) { colIndex += 1; objTable.Cell(1, colIndex).Range.Select(); this.SetAlignment("Center"); if (dc.ColumnName == "name") { objTable.Cell(1, colIndex).Range.InsertAfter("名称"); objTable.Columns[colIndex].PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent; objTable.Columns[colIndex].PreferredWidth = 25F; } else if (dc.ColumnName == "amount") { objTable.Cell(1, colIndex).Range.InsertAfter("数量"); objTable.Columns[colIndex].PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent; objTable.Columns[colIndex].PreferredWidth = 10F; } else if (dc.ColumnName == "source_amount") { objTable.Cell(1, colIndex).Range.InsertAfter("原值"); objTable.Columns[colIndex].PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent; objTable.Columns[colIndex].PreferredWidth = 15F; } else if (dc.ColumnName == "specification") { objTable.Cell(1, colIndex).Range.InsertAfter("编号(型号)"); objTable.Columns[colIndex].PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent; objTable.Columns[colIndex].PreferredWidth = 25F; } else if (dc.ColumnName == "invoice_num") { objTable.Cell(1, colIndex).Range.InsertAfter("发票号码"); objTable.Columns[colIndex].PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent; objTable.Columns[colIndex].PreferredWidth = 25F; } } } else if (ObjData.Tables[0].TableName == "project_contract_car_element") { foreach (DataColumn dc in dt.Columns) { colIndex += 1; objTable.Cell(1, colIndex).Range.Select(); this.SetAlignment("Center"); if (dc.ColumnName == "car_owner") { objTable.Cell(1, colIndex).Range.InsertAfter("机动车所有人"); } else if (dc.ColumnName == "car_type") { objTable.Cell(1, colIndex).Range.InsertAfter("车辆类型"); } else if (dc.ColumnName == "car_register_num") { objTable.Cell(1, colIndex).Range.InsertAfter("机动车登记编号"); } else if (dc.ColumnName == "car_num") { objTable.Cell(1, colIndex).Range.InsertAfter("车牌号码"); } else if (dc.ColumnName == "car_brand_num") { objTable.Cell(1, colIndex).Range.InsertAfter("品牌型号"); } } } //创建数据行 rowIndex = 1; foreach (DataRow dr in dt.Rows) { rowIndex += 1; colIndex = 0; foreach (DataColumn dc in dr.Table.Columns) { colIndex += 1; objTable.Cell(rowIndex, colIndex).Range.InsertAfter(dr[dc.ColumnName].ToString()); objTable.Cell(rowIndex, colIndex).Range.Select(); this.SetAlignment("Center"); } } //设置表格格式 //objTable.Borders.InsideLineStyle = 1; //objTable.Borders.OutsideLineStyle = 1; } //// this function doesn't work //public void InsertPageNumber( string strType, bool bHeader ) //{ // object missing = System.Reflection.Missing.Value; // object alignment ; // object bFirstPage = false; // object bF = true; // //if (bHeader == true) // //WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber = bF; // switch (strType) // { // case "Center": // alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter; // //WordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage); // //Word.Selection objSelection = WordApplic.pSelection; // oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter; // break; // case "Right": // alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight; // oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight; // break; // case "Left": // alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft; // oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage); // break; // } //} }
/// <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();//退出应用程序 } } }
public Block(Word.Table myTable, int startRowCount) { this.myTable = myTable; this.startRowCount = startRowCount; }
public void ExportData(DataGridView srcDgv, string fileName) //导出数据,传入一个datagridview和一个文件路径 { string type = fileName.Substring(fileName.IndexOf(".") + 1); //获得数据类型 if (type.Equals("xls", StringComparison.CurrentCultureIgnoreCase)) //Excel文档 { Excel.Application excel = new Excel.Application(); try { excel.DisplayAlerts = false; excel.Workbooks.Add(true); excel.Visible = false; for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题 { excel.Cells[2, i + 1] = srcDgv.Columns[i].HeaderText; } for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据 { for (int j = 0; j < srcDgv.Columns.Count; j++) { if (srcDgv[j, i].ValueType.ToString() == "System.Byte[]") { excel.Cells[i + 3, j + 1] = "System.Byte[]"; } else { excel.Cells[i + 3, j + 1] = srcDgv[j, i].Value; } } } excel.Workbooks[1].SaveCopyAs(fileName);//保存 } finally { excel.Quit(); } return; } //保存Word文件 if (type.Equals("doc", StringComparison.CurrentCultureIgnoreCase)) { object path = fileName; Object none = System.Reflection.Missing.Value; Word.Application wordApp = new Word.Application(); Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none); //建立表格 Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count + 1, srcDgv.Columns.Count, ref none, ref none); try { for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题 { table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText; } for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据 { for (int j = 0; j < srcDgv.Columns.Count; j++) { string a = srcDgv[j, i].ValueType.ToString(); if (a == "System.Byte[]") { PictureBox pp = new PictureBox(); byte[] pic = (byte[])(srcDgv[j, i].Value); //将数据库中的图片转换成二进制流 MemoryStream ms = new MemoryStream(pic); //将字节数组存入到二进制流中 pp.Image = Image.FromStream(ms); //二进制流Image控件中显示 pp.Image.Save(@"C:\22.bmp"); //将图片存入到指定的路径 object aaa = table.Cell(i + 2, j + 1).Range; wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; wordApp.Selection.InlineShapes.AddPicture(@"C:\22.bmp", ref none, ref none, ref aaa); pp.Dispose(); } else { table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString(); } } } document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none); document.Close(ref none, ref none, ref none); if (File.Exists(@"C:\22.bmp")) { File.Delete(@"C:\22.bmp"); } } finally { wordApp.Quit(ref none, ref none, ref none); } } }