public void Render(Microsoft.Office.Interop.Word.Table table) { table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; table.Range.Font.Name = "Calibri"; // 填入表格欄位(Column)名稱 for (int i = 0; i < table.Columns.Count; i++) { table.Cell(1, i + 1).Range.Text = MyDataTable.Columns[i].ColumnName; } // 填入表格資料 int rowIndex = 2; foreach (DataRow row in MyDataTable.Rows) { int colIndex = 1; foreach (DataColumn col in MyDataTable.Columns) { table.Cell(rowIndex, colIndex).Range.Text = row[col.ColumnName].ToString(); colIndex++; } rowIndex++; } }
void AddToCell(Microsoft.Office.Interop.Word.Table table, int rowindex, int colindex, float fontsize, int bold, string text) { table.Cell(rowindex, colindex).Range.Text = text; table.Cell(rowindex, colindex).Range.Font.Bold = bold; table.Cell(rowindex, colindex).Range.Font.Name = "verdana"; table.Cell(rowindex, colindex).Range.Font.Size = fontsize; table.Cell(rowindex, colindex).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; }
private void AddEducationInfo(ref Microsoft.Office.Interop.Word.Table tb) { int i = 0; foreach (EducationExperience educationExperience in _Employee.EmployeeDetails.Education.EducationExperiences) { tb.Cell(_EducationStratRow + i, 1).Range.Text = educationExperience.ExperiencePeriod; tb.Cell(_EducationStratRow + i, 2).Range.Text = educationExperience.Place; tb.Cell(_EducationStratRow + i, 3).Range.Text = educationExperience.Contect; i++; } }
//</Snippet2> //--------------------------------------------------------------------- //<Snippet3> private void AddData(System.Data.DataRow row, string companyName) { object missing = System.Type.Missing; // Create a table if it doesn't already exist. if (Globals.ThisDocument.Tables.Count == 0) { try { // Create a table. Microsoft.Office.Interop.Word.Table tbl = Globals.ThisDocument.Tables.Add (Globals.ThisDocument.Application.Selection.Range, 1, 4, ref missing, ref missing); // Insert headings. SetHeadings(tbl.Cell(1, 1), "Company Name"); SetHeadings(tbl.Cell(1, 2), "Product Name"); SetHeadings(tbl.Cell(1, 3), "Quantity"); SetHeadings(tbl.Cell(1, 4), "Unit Price"); } catch (Exception ex) { MessageBox.Show("Problem creating Products table: " + ex.Message, "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // Add data from data row to the table. Microsoft.Office.Interop.Word.Selection selection = Globals.ThisDocument.Application.Selection; if (selection.Tables.Count > 0) { Microsoft.Office.Interop.Word.Row newRow = Globals.ThisDocument.Tables[1].Rows.Add(ref missing); newRow.Range.Font.Bold = 0; newRow.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; newRow.Cells[4].Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; newRow.Cells[1].Range.Text = companyName; newRow.Cells[2].Range.Text = row["ProductName"].ToString(); newRow.Cells[3].Range.Text = row["QuantityPerUnit"].ToString(); newRow.Cells[4].Range.Text = Math.Round(Convert.ToDouble(row["UnitPrice"])).ToString("#,##0.00"); } else { MessageBox.Show("Cursor must be within a table.", "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void AddWorkInfo(ref Microsoft.Office.Interop.Word.Table tb) { int i = 0; foreach (WorkExperience workExperience in _Employee.EmployeeDetails.Work.WorkExperiences) { tb.Cell(_WorkStartRow + i, 1).Range.Text = workExperience.ExperiencePeriod; tb.Cell(_WorkStartRow + i, 2).Range.Text = workExperience.Place; tb.Cell(_WorkStartRow + i, 3).Range.Text = workExperience.ContactPerson; tb.Cell(_WorkStartRow + i, 4).Range.Text = workExperience.Contect; tb.Cell(_WorkStartRow + i, 5).Range.Text = workExperience.Remark; i++; } }
private void AddFamilyInfo(ref Microsoft.Office.Interop.Word.Table tb) { int i = 0; foreach (FamilyMember familyMember in _Employee.EmployeeDetails.Family.FamilyMembers) { tb.Cell(_FamilyStartRow + i, 1).Range.Text = familyMember.Name; tb.Cell(_FamilyStartRow + i, 2).Range.Text = familyMember.Relationship; tb.Cell(_FamilyStartRow + i, 3).Range.Text = familyMember.Age.ToString(); tb.Cell(_FamilyStartRow + i, 4).Range.Text = familyMember.Company; tb.Cell(_FamilyStartRow + i, 5).Range.Text = familyMember.Remark; i++; } }
//填充学生报告的基本信息 public static Microsoft.Office.Interop.Word.Table BaseInfo(Microsoft.Office.Interop.Word.Table tb1, Entity.GaoKaoCard infoGaoKaoCard, Entity.Join_Student infoStudent) { string strProvinceName = ""; Entity.Province infoProvince = DAL.Province.ProvinceEntityGet(infoStudent.ProvinceId); if (infoProvince != null) { strProvinceName = infoProvince.ProvinceName; } if (tb1.Rows.Count == 4) { //在指定单元格填值 //第1行 tb1.Cell(1, 2).Range.Text = infoStudent.StudentName; tb1.Cell(1, 4).Range.Text = (infoStudent.Sex == 1 ? "女" : "男"); //第2行 tb1.Cell(2, 2).Range.Text = infoStudent.SchoolName; tb1.Cell(2, 4).Range.Text = (infoStudent.WenLi == 1 ? "文史" : "理工"); //第3行 tb1.Cell(3, 2).Range.Text = strProvinceName; tb1.Cell(3, 4).Range.Text = infoStudent.GKYear.ToString(); //第4行 tb1.Cell(4, 2).Range.Text = infoGaoKaoCard.KaHao; tb1.Cell(4, 4).Range.Text = Basic.TypeConverter.StrToDateStr(DateTime.Now.ToString()); } return(tb1); }
public void Render(Microsoft.Office.Interop.Word.Table table) { table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; table.Range.Font.Name = "Calibri"; // 填入表格資料 int rowIndex = 1; foreach (DataRow row in MyDataTable.Rows) { ////table.Cell(rowIndex, 1).Range.Font.Bold = 1; ////table.Cell(rowIndex, 1).Range.Text = string.Format("{0}{1}({2})", //// row["Project"].ToString(), //// Environment.NewLine, //// row["Skus"].ToString()); Microsoft.Office.Interop.Word.Range range = table.Cell(rowIndex, 1).Range; Microsoft.Office.Interop.Word.Paragraph p1 = range.Paragraphs.Add(range); p1.Range.Font.Bold = 0; p1.Range.Font.Size = 10; p1.Range.Text = string.Format("({0})", row["Skus"].ToString()); Microsoft.Office.Interop.Word.Paragraph p2 = range.Paragraphs.Add(range); p2.Range.Font.Bold = 1; p2.Range.Font.Size = 12; p2.Range.Text = row["Project"].ToString() + Environment.NewLine; rowIndex++; } }
private void ExportAssessSystemInfo(ref Microsoft.Office.Interop.Word.Table tb) { decimal grade = 0; for (int i = 0; i < _ItemCount; i++) { //todo yyb AssessActivityItem managerItem = _AssessActivity.ItsAssessActivityPaper.ItsAssessActivityItems[i]; if (managerItem.Classfication == ItemClassficationEmnu._360) { continue; } tb.Cell(6 + i, 1).Range.Text = AssessSystemUtility.GetItemClassficationNameByItemClassfication(managerItem.Classfication); string questionAndNote = managerItem.Question; if (!string.IsNullOrEmpty(managerItem.Note)) { questionAndNote += "(" + managerItem.Note + ")"; } if (managerItem.AssessTemplateItemType == AssessTemplateItemType.Option) { tb.Cell(6 + i, 2).Range.Text = questionAndNote; //如果不是1到5分将存在问题 string[] options = managerItem.Option.Split('/'); int gradecellindex = managerItem.Grade < 6 ? Decimal.ToInt32(managerItem.Grade) : Decimal.ToInt32(managerItem.Grade) / 20; tb.Cell(6 + i, 3).Range.Text = options[5 - gradecellindex]; tb.Cell(6 + i, 4).Range.Text = (managerItem.Grade * managerItem.Weight).ToString(); } else if (managerItem.AssessTemplateItemType == AssessTemplateItemType.Open) { tb.Cell(6 + i, 2).Range.Text = managerItem.Question; tb.Cell(6 + i, 3).Range.Text = managerItem.Note; tb.Cell(6 + i, 4).Range.Text = "不计分"; } else { tb.Cell(6 + i, 2).Range.Text = questionAndNote; tb.Cell(6 + i, 4).Range.Text = (managerItem.Grade * managerItem.Weight).ToString(); } grade += (managerItem.Grade * managerItem.Weight); } tb.Cell(6 + _ItemCount, 4).Range.Text = grade.ToString(); }
/// <summary> /// 导出Word 的方法 /// </summary> private void tslExport_Word() { SaveFileDialog sfile = new SaveFileDialog(); sfile.AddExtension = true; sfile.DefaultExt = ".doc"; sfile.Filter = "(*.doc)|*.doc"; sfile.FileName = "理文检测系统抽包统计Word报表" + DateTime.Now.ToShortDateString(); if (sfile.ShowDialog() == DialogResult.OK) { object path = sfile.FileName; Object none = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none); //建立表格 Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, lvwUserList.Rows.Count + 1, lvwUserList.Columns.Count, ref none, ref none); try { for (int i = 0; i < lvwUserList.Columns.Count; i++)//设置标题 { table.Cell(0, i + 1).Range.Text = lvwUserList.Columns[i].HeaderText; } for (int i = 1; i < lvwUserList.Rows.Count; i++)//填充数据 { for (int j = 0; j < lvwUserList.Columns.Count; j++) { //table.Cell(i + 1, j + 1).Range.Text = dgvCarStatisticsQC[j, i - 1].Value.ToString(); if (lvwUserList[j, i - 1].Value != null) { table.Cell(i + 1, j + 1).Range.Text = lvwUserList[j, i - 1].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, ref none, ref none, ref none, ref none, ref none); document.Close(ref none, ref none, ref none); MessageBox.Show("导出数据成功!"); } finally { wordApp.Quit(ref none, ref none, ref none); } } }
// Запись в Word public static void ZapisWordIsx(int length, int[] mas, string title) { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); var Wrd = new Microsoft.Office.Interop.Word.Application(); Wrd.Visible = true; var inf = Type.Missing; String str; var Doc = Wrd.Documents.Add(inf, inf, inf, inf); Wrd.Selection.TypeText(title); Object t1 = Microsoft.Office.Interop.Word.WdDefaultTableBehavior.wdWord9TableBehavior; Object t2 = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent; Microsoft.Office.Interop.Word.Table tbl = Wrd.ActiveDocument.Tables.Add(Wrd.Selection.Range, 2, length, t1, t2); for (int i = 0; i < length; i++) { tbl.Cell(1, i + 1).Range.InsertAfter("[" + Convert.ToString(i) + "]"); str = String.Format("{0:f0}", mas[i]); tbl.Cell(2, i + 1).Range.InsertAfter(str); } }
public void _DesginTable(ref Microsoft.Office.Interop.Word.Table table, List <int> columnWeight) { for (int i = 0; i < table.Rows.Count; i++) { for (int j = 0; j < table.Columns.Count; j++) { table.Cell(i + 1, j + 1).Width = columnWeight[j]; } } }
//首席 public string Addchiefsupervisordata(WordTableInfo Info) { Common.Common.load_cheif_supervisor(); object missingValue = System.Reflection.Missing.Value; object myTrue = false; //不显示Word窗口 object fileName = Environment.CurrentDirectory + "\\" + "chief_supervisor.doc"; //WORD文档所在路径 string newfile = Common.Common.strAddfilesPath + Info.Teacher + Info.Time.Trim() + Info.Supervisor + ".doc"; //存储路径名称 // object fileName1 = Environment.CurrentDirectory + "\\" + "supervisor.doc"; Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word._Document oDoc; oDoc = oWord.Documents.Open(ref fileName, ref missingValue, ref myTrue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue); Microsoft.Office.Interop.Word.Table newtable = oDoc.Tables[1];//获取word文档中的表格 newtable.Cell(1, 2).Range.Text = Info.Teacher; newtable.Cell(1, 6).Range.Text = Info.Time.Substring(0, Info.Time.IndexOf(" ")); newtable.Cell(2, 6).Range.Text = Info.Classroom + Info.Time.Substring(Info.Time.IndexOf(" ") + 1); newtable.Cell(4, 2).Range.Text = Info.Class; newtable.Cell(5, 2).Range.Text = Info.Subject; object bSaveChange = true; oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue); oDoc = null; oWord = null; closefile(); if (!System.IO.File.Exists(Common.Common.strAddfilesPath)) { Directory.CreateDirectory(Common.Common.strAddfilesPath); } System.IO.File.Copy(fileName.ToString(), newfile, true); File.Delete(fileName.ToString()); //sent_email(Supervisor, Time, Subject, newfile); //movetofile(newfile); return(newfile); }
private void button13_Click(object sender, EventArgs e) { object filename = Application.StartupPath + "\\bin\\" + Global.templateName; Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDoc; wordDoc = wordApp.Documents.Open(filename); wordDoc.ActiveWindow.Visible = false; //打开word Microsoft.Office.Interop.Word.Table nowtable = wordDoc.Tables[17]; //检索表格 for (int i = 0; i < dataGridView4.RowCount - 1; i++) { for (int j = 0; j < dataGridView4.ColumnCount; j++) { nowtable.Cell(i + 2, j + 1).Range.InsertAfter(dataGridView4[j, i].Value.ToString());//填充表格 } } Microsoft.Office.Interop.Word.Table nowtable1 = wordDoc.Tables[8];//检索表格 for (int i = 0; i < 26; i++) { nowtable1.Cell(i + 3, 1).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable1.Cell(i + 3, 2).Range.InsertAfter(dataGridView1[1, i + 3].Value.ToString()); nowtable1.Cell(i + 3, 3).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable1.Cell(i + 3, 4).Range.InsertAfter(dataGridView1[2, i + 3].Value.ToString()); nowtable1.Cell(i + 3, 5).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable1.Cell(i + 3, 6).Range.InsertAfter(dataGridView1[3, i + 3].Value.ToString()); } Microsoft.Office.Interop.Word.Table nowtable2 = wordDoc.Tables[9];//检索表格 for (int i = 0; i < 26; i++) { nowtable2.Cell(i + 3, 1).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable2.Cell(i + 3, 2).Range.InsertAfter(dataGridView1[4, i + 3].Value.ToString()); nowtable2.Cell(i + 3, 3).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable2.Cell(i + 3, 4).Range.InsertAfter(dataGridView1[5, i + 3].Value.ToString()); nowtable2.Cell(i + 3, 5).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable2.Cell(i + 3, 6).Range.InsertAfter(dataGridView1[6, i + 3].Value.ToString()); } Microsoft.Office.Interop.Word.Table nowtable3 = wordDoc.Tables[10];//检索表格 for (int i = 0; i < 26; i++) { nowtable3.Cell(i + 3, 1).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable3.Cell(i + 3, 2).Range.InsertAfter(dataGridView1[7, i + 3].Value.ToString()); nowtable3.Cell(i + 3, 3).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable3.Cell(i + 3, 4).Range.InsertAfter(dataGridView1[8, i + 3].Value.ToString()); nowtable3.Cell(i + 3, 5).Range.InsertAfter(dataGridView1[0, i + 3].Value.ToString()); nowtable3.Cell(i + 3, 6).Range.InsertAfter(dataGridView1[9, i + 3].Value.ToString()); } wordDoc.Save(); wordApp.Quit(); wordApp = null; }
public static string GetWordPreview(string strFileName, bool boolVirtual = false, bool afterDelete = false) { string strPhysicalFilrName = boolVirtual ? HostingEnvironment.MapPath(strFileName) : strFileName; string strReturn = string.Empty; if (File.Exists(strPhysicalFilrName)) { object oFileName = strPhysicalFilrName; object oReadOnly = true; object oMissing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word._Application oWord; Microsoft.Office.Interop.Word._Document oDoc; oWord = new Microsoft.Office.Interop.Word.Application(); oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); string tableMessage = string.Empty; for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++) { Microsoft.Office.Interop.Word.Table nowTable = oDoc.Tables[tablePos]; tableMessage = tableMessage + string.Format("<br/>第{0}/{1}个表:<br/>", tablePos, oDoc.Tables.Count); for (int i = 1; i <= nowTable.Rows.Count; i++) { for (int j = 1; j <= nowTable.Columns.Count; j++) { try { string text = nowTable.Cell(i, j).Range.Text; tableMessage = tableMessage + text; tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2); //remove \r\a tableMessage = tableMessage + "(" + tablePos + "," + i + "," + j + ")"; tableMessage = tableMessage + "<br/>"; } catch (Exception) { continue; } } tableMessage = tableMessage + "<br/>"; } } strReturn = tableMessage.Replace("\r", "<br/>"); oDoc.Close(); oWord.Quit(); if (afterDelete) { File.Delete(strPhysicalFilrName); } } return(strReturn); }
public int TableIndexes(Dictionary <string, Index> indexDictionary, int pointer, Microsoft.Office.Interop.Word.Table wordTable) { if (indexDictionary.Count == 0) { return(pointer); } foreach (KeyValuePair <string, Index> index in indexDictionary) { object miss = System.Reflection.Missing.Value; wordTable.Rows.Add(ref miss); wordTable.Cell(pointer, 2).Merge(wordTable.Cell(pointer, 4)); string columns = string.Empty; foreach (System.Collections.Generic.KeyValuePair <string, string> indexColumn in index.Value.Columns) { columns += string.Format("{0} {1},", indexColumn.Key, indexColumn.Value); } columns = columns.TrimEnd(','); wordTable.Cell(pointer, 2).Range.Text = string.Format("{0}索引{1}({2}):({3})", index.Value.Uniqueness, index.Value.IndexName, index.Value.TableSpace, columns); pointer++; } if (indexDictionary.Count > 1) { wordTable.Cell(pointer - indexDictionary.Count, 1).Merge(wordTable.Cell(pointer - 1, 1)); } wordTable.Cell(pointer - indexDictionary.Count, 1).Range.Text = "索引"; return(pointer); }
private void cOSToolStripMenuItem_Click(object sender, EventArgs e) { DataTable coslist = DbHelperSQL.Query("select 文件地址,文件名 from paperwork where 文件类型='COS'").Tables[0]; List <string> creatName = new List <string>(); foreach (DataRow pp in coslist.Rows) { string drawingno = pp[1].ToString().Split('_')[0].Replace("\r\a", ""); myWordfile = wordMethod.opendoc(pp[0].ToString(), false); Microsoft.Office.Interop.Word.Table parttable = myWordfile.Tables[1]; int ptc = parttable.Rows.Count; for (int i = 2; i <= ptc; i++) { int k = 0; if (parttable.Cell(1, 1).Range.Text.Contains("序号")) { k = 1; } string partname = parttable.Cell(i, 3 + k).Range.Text.Replace("\r\a", ""); string partno = parttable.Cell(i, 1 + k).Range.Text.Replace("\r\a", ""); string partqty = parttable.Cell(i, 4 + k).Range.Text.Replace("\r\a", ""); StringBuilder strSqlname = new StringBuilder(); strSqlname.Append("INSERT INTO 零件列表 ("); strSqlname.Append("零件名称,零件图号,零件数量,产品图号"); strSqlname.Append(string.Format(") VALUES ('{0}','{1}',{2},'{3}') ON DUPLICATE KEY UPDATE 零件数量={2}", partname, partno, partqty, drawingno)); creatName.Add(strSqlname.ToString()); } } DbHelperSQL.ExecuteSqlTran(creatName); MessageBox.Show("执行成功"); }
private static void _CellsWrite(int[] posRC, Microsoft.Office.Interop.Word.Table myTable, IEnumerable <string> sb) { int i = 0; foreach (string str in sb) { while (!IsExistRow(myTable, posRC[0] + i)) { myTable.Rows.Add(); } myTable.Cell(posRC[0] + i, posRC[1]).Range.Text = str; i += 1; } }
private void button4_Click(object sender, EventArgs e) { #region 除后台word占用。 System.Diagnostics.Process myproc = new System.Diagnostics.Process(); //得到所有打开的进程 try { foreach (Process thisproc in Process.GetProcessesByName("WINWORD")) { if (!thisproc.CloseMainWindow()) { thisproc.Kill(); } } } catch (Exception) { MessageBox.Show("杀死" + "WINWORD" + "失败!"); } #endregion try { object filename = Environment.CurrentDirectory.ToString() + "\\bin\\" + Global.templateName; Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDoc; wordDoc = wordApp.Documents.Open(filename); wordDoc.ActiveWindow.Visible = false; //打开word Microsoft.Office.Interop.Word.Table nowtable = wordDoc.Tables[3]; //检索表格 for (int j = 0; j < dataGridView2.ColumnCount - 1; j++) { for (int i = 0; i < dataGridView2.RowCount - 1; i++) { nowtable.Cell(i + 2, j + 2).Range.InsertAfter(dataGridView2[j + 1, i].Value.ToString());//填充表格 } } wordDoc.Save(); wordApp.Quit(); wordApp = null; MessageBox.Show("导入成功。"); } catch (Exception) { MessageBox.Show("失败。"); } }
//填充学生报告的基本信息 public static Microsoft.Office.Interop.Word.Table BaseInfo(Microsoft.Office.Interop.Word.Table tb1, Entity.UserInfo userinfo, Entity.Join_Student user, string strKaHao) { if (tb1.Rows.Count == 4) { //在指定单元格填值 //第1行 tb1.Cell(1, 2).Range.Text = userinfo.StudentName; tb1.Cell(1, 4).Range.Text = (user.Sex == 1 ? "女" : "男"); //第2行 tb1.Cell(2, 2).Range.Text = user.SchoolName; tb1.Cell(2, 4).Range.Text = (userinfo.KeLei == 1 ? "文史" : "理工"); //第3行 tb1.Cell(3, 2).Range.Text = userinfo.ProvinceName; tb1.Cell(3, 4).Range.Text = user.GKYear.ToString(); //第4行 tb1.Cell(4, 2).Range.Text = strKaHao; tb1.Cell(4, 4).Range.Text = Basic.TypeConverter.StrToDateStr(DateTime.Now.ToString()); } return(tb1); }
private void button3_Click(object sender, EventArgs e) { object filename = Application.StartupPath.ToString() + "\\bin\\" + Global.templateName; Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDoc; wordDoc = wordApp.Documents.Open(filename); wordDoc.ActiveWindow.Visible = false; //打开word Microsoft.Office.Interop.Word.Table nowtable = wordDoc.Tables[15]; //检索表格 for (int i = 0; i < dataGridView1.RowCount - 1; i++) { for (int j = 0; j < dataGridView1.ColumnCount; j++) { nowtable.Cell(i + 2, j + 2).Range.InsertAfter(dataGridView1[j, i].Value.ToString());//填充表格 } } wordDoc.Save(); wordApp.Quit(); wordApp = null; }
private void button1_Click(object sender, EventArgs e) { object filename = Application.StartupPath + "\\bin\\" + Global.templateName; Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wordDoc; wordDoc = wordApp.Documents.Open(filename); wordDoc.ActiveWindow.Visible = false; Microsoft.Office.Interop.Word.Table nowtable2 = wordDoc.Tables[9]; nowtable2.Cell(3, 3).Range.InsertAfter(textBox1.Text); nowtable2.Cell(4, 3).Range.InsertAfter(textBox2.Text); nowtable2.Cell(5, 3).Range.InsertAfter(textBox3.Text); nowtable2.Cell(7, 3).Range.InsertAfter(textBox4.Text); nowtable2.Cell(8, 3).Range.InsertAfter(textBox5.Text); nowtable2.Cell(9, 3).Range.InsertAfter(textBox6.Text); nowtable2.Cell(2, 4).Range.InsertAfter(textBox7.Text); wordDoc.Save(); wordApp.Quit(); wordApp = null; }
private void AddEmployeePhoto(ref Microsoft.Office.Interop.Word.Table tb, ref Microsoft.Office.Interop.Word.Document doc) { if (_Employee.EmployeeDetails.Photo != null) { //插入图片 CreatePicture(); string FileName = _TempPhotolocation; object LinkToFile = false; object SaveWithDocument = true; tb.Cell(11, 5).Select(); object Anchor = doc.Application.Selection.Range; doc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); doc.Application.ActiveDocument.InlineShapes[2].Width = 78f; //图片宽度 doc.Application.ActiveDocument.InlineShapes[2].Height = 111f; //图片高度 //将图片设置为四周环绕型 Microsoft.Office.Interop.Word.Shape s = doc.Application.ActiveDocument.InlineShapes[2].ConvertToShape(); s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapFront; } }
public string Addsupervisordata(WordTableInfo Info) { Common.Common.load_supervisor(); object missingValue = System.Reflection.Missing.Value; object myTrue = false; //不显示Word窗口 //object fileName = Environment.CurrentDirectory + "\\" + "chief_supervisor.doc"; object fileName1 = Environment.CurrentDirectory + "\\" + "supervisor.doc"; string newfile = Common.Common.strAddfilesPath + "\\" + Info.Teacher + Info.Time.Trim() + Info.Supervisor + ".doc"; Microsoft.Office.Interop.Word._Application oWord1 = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word._Document oDoc1; oDoc1 = oWord1.Documents.Open(ref fileName1, ref missingValue, ref myTrue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue); Microsoft.Office.Interop.Word.Table newtable1 = oDoc1.Tables[1]; oWord1.Selection.TypeText("广东医学院教师课堂教学质量评价表" + "(" + Info.Teachingtype + ")"); newtable1.Cell(1, 2).Range.Text = Info.Teacher; newtable1.Cell(1, 4).Range.Text = Info.Perfession; newtable1.Cell(1, 6).Range.Text = Info.Time.Substring(0, Info.Time.IndexOf(" ")); newtable1.Cell(2, 2).Range.Text = Info.Class; newtable1.Cell(2, 4).Range.Text = Info.Classroom + Info.Time.Substring(Info.Time.IndexOf(" ") + 1); newtable1.Cell(3, 2).Range.Text = Info.Subject; object bSaveChange = true; oDoc1.Close(ref bSaveChange, ref missingValue, ref missingValue); oDoc1 = null; oWord1 = null; closefile(); if (!System.IO.File.Exists(Common.Common.strAddfilesPath)) { Directory.CreateDirectory(Common.Common.strAddfilesPath); } System.IO.File.Copy(fileName1.ToString(), newfile, true); File.Delete(fileName1.ToString()); return(newfile); }
public int TableTrigger(Dictionary <string, Trigger> triggerDictionary, int pointer, Microsoft.Office.Interop.Word.Table wordTable) { if (triggerDictionary.Count == 0) { return(pointer); } foreach (KeyValuePair <string, Trigger> trigger in triggerDictionary) { object miss = System.Reflection.Missing.Value; wordTable.Rows.Add(ref miss); wordTable.Cell(pointer, 2).Merge(wordTable.Cell(pointer, 4)); wordTable.Cell(pointer, 2).Range.Text = string.Format("{0}({1}):\n({2})", trigger.Value.TriggerName, trigger.Value.TriggeringEvent, trigger.Value.TriggerBody); pointer++; } if (triggerDictionary.Count > 1) { wordTable.Cell(pointer - triggerDictionary.Count, 1).Merge(wordTable.Cell(pointer - 1, 1)); } wordTable.Cell(pointer - triggerDictionary.Count, 1).Range.Text = "触发器"; return(pointer); }
private void create_Click(object sender, EventArgs e) { int numColumns = 2; int numRows = 5; // create document Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); object missing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Document writer = wordApp.Documents.Add( ref missing, ref missing, ref missing, ref missing); writer = configureDocument(writer); if (type.Equals("Dalam Negeri")) { writer.Content.Text = "Kerjasama dalam negeri"; } else if (type.Equals("Luar Negeri")) { writer.Content.Text = "Kerjasama luar negeri"; } writer.Range(0, 22).Bold = 1; writer.Range(0, 22).Font.Size = 16; writer.Range(0, 22).ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; writer.Range(0, 22).ParagraphFormat.SpaceAfter = 24; // table configuration object endOfDoc = "\\endofdoc"; Microsoft.Office.Interop.Word.Range rangeOfWord = writer.Bookmarks.get_Item(endOfDoc).Range; Microsoft.Office.Interop.Word.Table table = writer.Tables.Add(rangeOfWord, numRows, numColumns, ref missing, ref missing); table.Range.ParagraphFormat.SpaceAfter = 0; table.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; table.Range.Font.Size = 11; table.Range.Bold = 0; table.Columns[1].PreferredWidth = 200; table.Columns[2].PreferredWidth = 550; for (int i = 1; i <= numRows; i++) { for (int j = 1; j <= numColumns; j++) { table.Cell(i, j).Range.Borders.Enable = 1; } } wordApp.Visible = false; wordApp.ShowAnimation = false; // populating table with data // save document Object savedir = path + "\\" + relTitleTextBox.Text + "_" + instantTextBox.Text + "_"; writer.SaveAs2(ref savedir); writer.Close(ref missing, ref missing, ref missing); wordApp.Quit(ref missing, ref missing, ref missing); MessageBox.Show("Document saved"); parent.DocumentList_Load(sender, e); }
public static void ExportWordByMicrosoftOfficeInteropWord(string databaseName, List <TableDto> tables) { string docTitle = "数据库名:" + databaseName; object template = System.Reflection.Missing.Value; object oEndOfDoc = @"\endofdoc"; // \endofdoc是预定义的bookmark // TODO 依赖冲突,所以用了全类名 Microsoft.Office.Interop.Word._Application application = new Microsoft.Office.Interop.Word.Application(); application.Visible = false; Microsoft.Office.Interop.Word._Document document = application.Documents.Add(ref template, ref template, ref template, ref template); application.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView; application.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; application.ActiveWindow.ActivePane.Selection.InsertAfter("DBCHM https://gitee.com/lztkdr/DBCHM"); application.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; application.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; Microsoft.Office.Interop.Word.Paragraph paragraph = document.Content.Paragraphs.Add(ref template); paragraph.Range.Text = docTitle; paragraph.Range.Font.Bold = 1; paragraph.Range.Font.Name = "宋体"; paragraph.Range.Font.Size = 12f; paragraph.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph.Format.SpaceAfter = 5f; paragraph.OutlineLevel = Microsoft.Office.Interop.Word.WdOutlineLevel.wdOutlineLevel1; paragraph.Range.InsertParagraphAfter(); // TODO 遍历数据库表集合 foreach (var table in tables) { string docTableName = table.TableName + " " + (!string.IsNullOrWhiteSpace(table.Comment) ? table.Comment : ""); // TODO 一级标题 object oRng = document.Bookmarks[oEndOfDoc].Range; Microsoft.Office.Interop.Word.Paragraph paragraph2 = document.Content.Paragraphs.Add(ref oRng); paragraph2.Range.Text = docTableName; paragraph2.Range.Font.Bold = 1; paragraph2.Range.Font.Name = "宋体"; paragraph2.Range.Font.Size = 10f; paragraph2.OutlineLevel = Microsoft.Office.Interop.Word.WdOutlineLevel.wdOutlineLevel2; paragraph2.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; paragraph2.Format.SpaceBefore = 15f; paragraph2.Format.SpaceAfter = 5f; paragraph2.Range.InsertParagraphAfter(); // TODO 遍历数据库表字段集合 // TODO 创建表格 Microsoft.Office.Interop.Word.Range range = document.Bookmarks[oEndOfDoc].Range; Microsoft.Office.Interop.Word.Table table2 = document.Tables.Add(range, table.Columns.Count + 1, 10, ref template, ref template); table2.Range.Font.Name = "宋体"; table2.Range.Font.Bold = 0; table2.Range.Font.Size = 9f; table2.Borders.Enable = 1; table2.Rows.Height = 10f; table2.AllowAutoFit = true; table2.Cell(1, 1).Range.Text = "序号"; table2.Cell(1, 2).Range.Text = "列名"; table2.Cell(1, 3).Range.Text = "数据类型"; table2.Cell(1, 4).Range.Text = "长度"; table2.Cell(1, 5).Range.Text = "小数位"; table2.Cell(1, 6).Range.Text = "主键"; table2.Cell(1, 7).Range.Text = "自增"; table2.Cell(1, 8).Range.Text = "允许空"; table2.Cell(1, 9).Range.Text = "默认值"; table2.Cell(1, 10).Range.Text = "列说明"; // TODO 分别设置word文档中表格的列宽 int j = 0; foreach (var column in table.Columns) { table2.Cell(j + 2, 1).Range.Text = column.ColumnOrder; table2.Cell(j + 2, 2).Range.Text = column.ColumnName; table2.Cell(j + 2, 3).Range.Text = column.ColumnTypeName; table2.Cell(j + 2, 4).Range.Text = column.Length; table2.Cell(j + 2, 5).Range.Text = column.Scale; table2.Cell(j + 2, 6).Range.Text = column.IsPK; table2.Cell(j + 2, 7).Range.Text = column.IsIdentity; table2.Cell(j + 2, 8).Range.Text = column.CanNull; table2.Cell(j + 2, 9).Range.Text = column.DefaultVal; table2.Cell(j + 2, 10).Range.Text = column.Comment; j++; } } application.Visible = true; document.Activate(); }
private void ExportBasicInfo(ref Microsoft.Office.Interop.Word.Table tb) { tb.Cell(1, 3).Range.Text = AssessSystemUtility.GetCharacterNameByType(_AssessActivity.AssessCharacterType); tb.Cell(1, 5).Range.Text = _AssessActivity.ScopeFrom.ToShortDateString() + " 至 " + _AssessActivity.ScopeTo.ToShortDateString(); tb.Cell(2, 2).Range.Text = _AssessActivity.ItsEmployee.Account.Name; tb.Cell(2, 4).Range.Text = _AssessActivity.ItsEmployee.EmployeeDetails.Gender.Name; tb.Cell(2, 6).Range.Text = _AssessActivity.ItsEmployee.EmployeeDetails.Birthday.ToShortDateString(); tb.Cell(3, 2).Range.Text = _AssessActivity.ItsEmployee.Account.Dept.DepartmentName; tb.Cell(3, 4).Range.Text = _AssessActivity.ItsEmployee.Account.Position.Name; tb.Cell(3, 6).Range.Text = _AssessActivity.ItsEmployee.Account.Dept.DepartmentLeader.Name; tb.Cell(4, 2).Range.Text = _AssessActivity.ItsEmployee.EmployeeDetails.Education.EducationalBackground.Name; tb.Cell(4, 4).Range.Text = _AssessActivity.ItsEmployee.EmployeeDetails.Work.ComeDate.ToShortDateString(); string PersonalChoose = ""; if (_AssessActivity != null && _AssessActivity.ItsAssessActivityPaper != null && _AssessActivity.ItsAssessActivityPaper.SubmitInfoes != null) { foreach (SubmitInfo info in _AssessActivity.ItsAssessActivityPaper.SubmitInfoes) { if (info.SubmitInfoType.Id == SubmitInfoType.MyselfAssess.Id) { PersonalChoose = info.Choose; } } } tb.Cell(7, 2).Range.Text = PersonalChoose; }
public void print_report_2(DataTable dt, DateTime d1, DateTime d2) { try { object missing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Document report_file = win_word.Documents.Add(ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.Paragraph para1 = report_file.Content.Paragraphs.Add(ref missing); para1.Range.Text = String.Format("Отчет с {0} по {1}", d1.ToShortDateString(), d2.ToShortDateString()); para1.Range.InsertParagraphAfter(); Microsoft.Office.Interop.Word.Table data_table = report_file.Tables.Add(para1.Range, dt.Rows.Count + 1, 9, ref missing, ref missing); data_table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; data_table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; data_table.Cell(1, 1).Range.Text = "Дата"; data_table.Cell(1, 2).Range.Text = "ФИО клиента"; data_table.Cell(1, 3).Range.Text = "Код услуги"; data_table.Cell(1, 4).Range.Text = "Цена"; data_table.Cell(1, 5).Range.Text = "Скидка"; data_table.Cell(1, 6).Range.Text = "Итог"; data_table.Cell(1, 7).Range.Text = "Доктор"; data_table.Cell(1, 8).Range.Text = "Доля"; data_table.Cell(1, 9).Range.Text = "Админ"; //logs(date datetime default CURRENT_TIMESTAMP, name text, service_code int, price int, discount int, final int, doctorcode text, share int, login text);"); //fill table int index = 2; foreach (DataRow dr in dt.Rows) { data_table.Cell(index, 1).Range.Text = dr["date"].ToString(); data_table.Cell(index, 2).Range.Text = dr["name"].ToString(); data_table.Cell(index, 3).Range.Text = dr["service_code"].ToString(); data_table.Cell(index, 4).Range.Text = dr["price"].ToString(); data_table.Cell(index, 5).Range.Text = dr["discount"].ToString(); data_table.Cell(index, 6).Range.Text = dr["final"].ToString(); data_table.Cell(index, 7).Range.Text = dr["doctorcode"].ToString(); data_table.Cell(index, 8).Range.Text = dr["share"].ToString(); data_table.Cell(index, 9).Range.Text = dr["login"].ToString(); index++; } SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "doc files (*.docx)|*.docx|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; object file_path = ""; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { file_path = saveFileDialog1.FileName; } report_file.SaveAs2(ref file_path); report_file.Close(ref missing, ref missing, ref missing); report_file = null; MessageBox.Show("Document created successfully !"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private static int CellWrite(int row, int column, Microsoft.Office.Interop.Word.Table myTable, string txt) { myTable.Cell(row, column).Range.Text = txt; return(1); }
/// <summary> /// Находим таблицу со строкой /// </summary> /// <param name="parString">Параметр - строка</param> public void GetTableWithString(String parString) { String st = String.Empty; int i = 1; do { table = document.Tables[i]; st = table.Cell(1, 1).Range.Text; i++; } while (!st.Contains("№")); }