/// <summary> /// 分离单元格 /// </summary> public static void SeparateCell() { object oMissing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word._Application WordApp; Microsoft.Office.Interop.Word._Document WordDoc; WordApp = new Microsoft.Office.Interop.Word.Application(); WordApp.Visible = true; WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Microsoft.Office.Interop.Word.Range tableLocation = WordDoc.Range(ref start, ref end); WordDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Microsoft.Office.Interop.Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2)); object Rownum = 2; object Columnnum = 2; cell.Split(ref Rownum, ref Columnnum); }
private void button5_Click(object sender, EventArgs e) { Word.Document WordDoc; var WordApp = new Word.Application(); WordApp.Visible = true; WordDoc = WordApp.Documents.Add(Type.Missing, false, Word.WdNewDocumentType.wdNewBlankDocument, true); Word.Range wordrange = WordDoc.Range(0, 0); Word.Table wordtable = WordDoc.Tables.Add(wordrange, dataGridView1.RowCount, dataGridView1.ColumnCount, Word.WdDefaultTableBehavior.wdWord9TableBehavior, Word.WdAutoFitBehavior.wdAutoFitWindow); Word.Range wordcellrange = WordDoc.Tables[1].Cell(1, 1).Range; wordcellrange.Text = "№"; wordcellrange = wordtable.Cell(1, 2).Range; wordcellrange.Text = "Классификатор"; wordcellrange = wordtable.Cell(1, 3).Range; wordcellrange.Text = "Диагноз"; wordcellrange = wordtable.Cell(1, 4).Range; wordcellrange.Text = "Стоимость лечения"; for (int m = 1; m < wordtable.Rows.Count; m++) { for (int n = 0; n < wordtable.Columns.Count; n++) { wordcellrange = wordtable.Cell(m + 1, n + 1).Range; wordcellrange.Text = dataGridView1[n, m - 1].Value.ToString(); } } }
/// <summary> /// 合并单元格 /// </summary> public static void CombinationCell() { object oMissing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word._Application WordApp; Microsoft.Office.Interop.Word._Document WordDoc; WordApp = new Microsoft.Office.Interop.Word.Application(); WordApp.Visible = true; WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Microsoft.Office.Interop.Word.Range tableLocation = WordDoc.Range(ref start, ref end); WordDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Microsoft.Office.Interop.Word.Cell cell = newTable.Cell(2, 1);//2行1列合并2行2列为一起 cell.Merge(newTable.Cell(2, 2)); //cell.Merge( newTable.Cell( 1, 3 ) ); }
/// <summary> /// word中添加新表 /// </summary> public static void AddTable() { object oMissing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word._Application WordApp; Microsoft.Office.Interop.Word._Document WordDoc; WordApp = new Microsoft.Office.Interop.Word.Application(); WordApp.Visible = true; WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Microsoft.Office.Interop.Word.Range tableLocation = WordDoc.Range(ref start, ref end); WordDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);//3行4列的表 }
private void button5_Click(object sender, EventArgs e) { Word.Document WordDoc; var WordApp = new Word.Application(); WordApp.Visible = true; //object template = Type.Missing; //object newTemplate = false; //object documentType = Word.WdNewDocumentType.wdNewBlankDocument; //object visible = true; WordDoc = WordApp.Documents.Add(Type.Missing, false, Word.WdNewDocumentType.wdNewBlankDocument, true); //object start = 0; //object end = 0; Word.Range wordrange = WordDoc.Range(0, 0); object defaultTableBehavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior; object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow; Word.Table wordtable = WordDoc.Tables.Add(wordrange, dataGridView1.RowCount, dataGridView1.ColumnCount, ref defaultTableBehavior, ref autoFitBehavior); Word.Range wordcellrange = WordDoc.Tables[1].Cell(1, 1).Range; wordcellrange.Text = "№"; wordcellrange = wordtable.Cell(1, 2).Range; wordcellrange.Text = "Фамилия"; wordcellrange = wordtable.Cell(1, 3).Range; wordcellrange.Text = "Имя"; wordcellrange = wordtable.Cell(1, 4).Range; wordcellrange.Text = "Отчество"; wordcellrange = wordtable.Cell(1, 5).Range; wordcellrange.Text = "Должность"; wordcellrange = wordtable.Cell(1, 6).Range; wordcellrange.Text = "Дата рождения"; wordcellrange = wordtable.Cell(1, 7).Range; wordcellrange.Text = "Дата приема на работу"; wordcellrange = wordtable.Cell(1, 8).Range; wordcellrange.Text = "Номер кабинета"; for (int m = 1; m < wordtable.Rows.Count; m++) { for (int n = 0; n < wordtable.Columns.Count; n++) { wordcellrange = wordtable.Cell(m + 1, n + 1).Range; wordcellrange.Text = dataGridView1[n, m - 1].Value.ToString(); } } }
/// <summary> /// 添加一个新表 参考 /// </summary> public Table AddTable(int rows, int columns) { object oMissing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application WordApp; Microsoft.Office.Interop.Word.Document WordDoc; WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); WordApp.Visible = true; WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Microsoft.Office.Interop.Word.Range tableLocation = WordDoc.Range(ref start, ref end); Table newTable = WordDoc.Tables.Add(tableLocation, rows, columns, ref oMissing, ref oMissing);//3行4列的表 //设置表的格式 newTable.Borders.Enable = 1; //允许有边框,默认没有边框(为0时报错,1为实线边框,2、3为虚线边框,以后的数字没试过) newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt; //边框宽度 newTable.AllowPageBreaks = false; return(newTable); }
private void button1_Click(object sender, EventArgs e) { Word.Document WordDoc; var WordApp = new Word.Application(); WordApp.Visible = true; WordDoc = WordApp.Documents.Add(Type.Missing, false, Word.WdNewDocumentType.wdNewBlankDocument, true); Word.Range wordrange = WordDoc.Range(0, 0); Word.Table wordtable = WordDoc.Tables.Add(wordrange, 8, 2, Word.WdDefaultTableBehavior.wdWord9TableBehavior, Word.WdAutoFitBehavior.wdAutoFitWindow); Word.Range wordcellrange = WordDoc.Tables[1].Cell(1, 1).Range; wordcellrange.Text = "ФИО пациента:"; wordcellrange = wordtable.Cell(2, 1).Range; wordcellrange.Text = "Дата приема:"; wordcellrange = wordtable.Cell(3, 1).Range; wordcellrange.Text = "Диагноз:"; wordcellrange = wordtable.Cell(4, 1).Range; wordcellrange.Text = "Препарат:"; wordcellrange = wordtable.Cell(5, 1).Range; wordcellrange.Text = "Дополнительно:"; wordcellrange = wordtable.Cell(6, 1).Range; wordcellrange.Text = "Стоимость услуги:"; wordcellrange = wordtable.Cell(7, 1).Range; wordcellrange.Text = "Подпись врача:"; wordcellrange = wordtable.Cell(8, 1).Range; wordcellrange.Text = "Подпись пациента:"; wordcellrange = wordtable.Cell(1, 2).Range; wordcellrange.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); wordcellrange = wordtable.Cell(2, 2).Range; wordcellrange.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); wordcellrange = wordtable.Cell(3, 2).Range; wordcellrange.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString(); wordcellrange = wordtable.Cell(4, 2).Range; wordcellrange.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString(); wordcellrange = wordtable.Cell(5, 2).Range; wordcellrange.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString(); wordcellrange = wordtable.Cell(6, 2).Range; wordcellrange.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString() + " ₽"; }