VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之Word报告自动生成(例如 导出数据库结构(2)

{ 261 columns.Add(i); 262 } 263 } 264 } 265 columns.Sort(); 266 columnCount = columns.Count; 267 int rowsCount = dataRows.Length; 268 269 Word.ChartData chartData = chart.ChartData; 270 271 //chartData.Activate(); 272 //此处有个比较疑惑的问题, 不执行此条,生成的报告中的图表无法再次右键编辑数据. 执行后可以, 但有两个问题就是第一会弹出Excel框, 处理完后会自动关闭. 第二部分chart的数据range设置总不对 273 //不知道是不是版本的问题, 谁解决了分享一下,谢谢 274 275 Excel.Workbook dataWorkbook = (Excel.Workbook)chartData.Workbook; 276 dataWorkbook.Application.Visible = false; 277 278 Excel.Worksheet dataSheet = (Excel.Worksheet)dataWorkbook.Worksheets[1]; 279 //设定范围 280 string a = (chartElement.ColumnNameForHead ? rowsCount + 1 : rowsCount) + "|" + columnCount; 281 Console.WriteLine(a); 282 283 Excel.Range tRange = dataSheet.Range["A1", dataSheet.Cells[(chartElement.ColumnNameForHead ? rowsCount + 1 : rowsCount), columnCount]]; 284 Excel.ListObject tbl1 = dataSheet.ListObjects[1]; 285 //dataSheet.ListObjects[1].Delete(); //想过重新删除再添加 这样 原有数据清掉了, 但觉得性能应该会有所下降 286 //Excel.ListObject tbl1 = dataSheet.ListObjects.AddEx(); 287 tbl1.Resize(tRange); 288 for (int j = 0; j < rowsCount; j++) 289 { 290 DataRow row = dataRows[j]; 291 for (int k = 0; k < columnCount; k++) 292 { 293 dataSheet.Cells[j + 2, k + 1].FormulaR1C1 = row[columns[k]]; 294 } 295 } 296 297 if (chartElement.ColumnNameForHead) 298 { 299 for (int k = 0; k < columns.Count; k++) 300 { 301 dataSheet.Cells[1, k + 1].FormulaR1C1 = dataTable.Columns[columns[k]].ColumnName; 302 } 303 } 304 chart.ChartTitle.Text = chartElement.Name; 305 //dataSheet.Application.Quit(); 306 } 307 } 308 } 309 310 return true; 311 } 312 313 /// <summary> 314 /// 更新目录 315 /// </summary> 316 /// <returns></returns> 317 private bool UpdateTablesOfContents() 318 { 319 foreach (Word.TableOfContents item in wordDoc.TablesOfContents) 320 { 321 item.Update(); 322 } 323 324 return true; 325 } 326 327 /// <summary> 328 /// 保存文件 329 /// </summary> 330 /// <param name="newFilePath"></param> 331 /// <param name="newFileName"></param> 332 /// <param name="saveFormat"></param> 333 /// <returns></returns> 334 private bool SaveFile(string newFilePath, ref string newFileName, int saveFormat = 16) 335 { 336 if (string.IsNullOrEmpty(newFileName)) 337 { 338 newFileName = DateTime.Now.ToString("yyyyMMddHHmmss"); 339 340 switch (saveFormat) 341 { 342 case 0:// Word.WdSaveFormat.wdFormatDocument 343 newFileName += ".doc"; 344 break; 345 case 16:// Word.WdSaveFormat.wdFormatDocumentDefault 346 newFileName += ".docx"; 347 break; 348 case 17:// Word.WdSaveFormat.wdFormatPDF 349 newFileName += ".pdf"; 350 break; 351 default: 352 break; 353 } 354 } 355 356 object newfile = Path.Combine(newFilePath, newFileName); 357 object wdSaveFormat = saveFormat; 358 wordDoc.SaveAs(ref newfile, ref wdSaveFormat); 359 return true; 360 } 361 362 /// <summary> 363 /// 清理 364 /// </summary> 365 private void CloseAndClear() 366 { 367 if (wordApp == null) 368 { 369 return; 370 } 371 wordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges); 372 wordApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges); 373 System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc); 374 System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); 375 wordDoc = null; 376 wordApp = null; 377 GC.Collect(); 378 KillProcess("Excel", "WINWORD"); 379 } 380 381 /// <summary> 382 /// 杀进程.. 383 /// </summary> 384 /// <param name="processNames"></param> 385 private void KillProcess(params string[] processNames) 386 { 387 //Process myproc = new Process(); 388 //得到所有打开的进程 389 try 390 { 391 foreach (string name in processNames) 392 { 393 foreach (Process thisproc in Process.GetProcessesByName(name)) 394 { 395 if (!thisproc.CloseMainWindow()) 396 { 397 if (thisproc != null) 398 thisproc.Kill(); 399 } 400 } 401 } 402 } 403 catch (Exception) 404 { 405 //throw Exc; 406 // msg.Text+= "杀死" + processName + "失败!"; 407 } 408 } 409 } 410 411 /// <summary> 412 /// 封装的Word元素 413 /// </summary> 414 public class WordElement 415 { 416 public WordElement(Word.Range range, string elementName = "", DataRow dataRow = null, Dictionary<string, string> groupBy = null, int tableIndex = 0) 417 { 418 this.Range = range; 419 this.ElementName = elementName; 420 this.GroupBy = groupBy; 421 this.DataRow = dataRow; 422 if (string.IsNullOrEmpty(elementName)) 423 { 424 this.Level = 0; 425 this.TableIndex = tableIndex; 426 this.Name = string.Empty; 427 this.ColumnNameForHead = false; 428 } 429 else 430 { 431 string[] element = elementName.Split('_'); 432 this.Level = int.Parse(element[1]); 433 this.ColumnNameForHead = false; 434 this.ColumnStart = -1; 435 this.ColumnEnd = -1; 436 437 if (element[0].Equals("label")) 438 { 439 this.Name = element[2]; 440 this.TableIndex = 0; 441 } 442 else 443 { 444 this.Name = element[4]; 445 this.TableIndex = int.Parse(element[2]) - 1; 446 447 if (!string.IsNullOrEmpty(element[3])) 448 { 449 string[] filters = element[3].Split(new string[] { "XX" }, StringSplitOptions.RemoveEmptyEntries); 450 if (this.GroupBy == null) 451 { 452 this.GroupBy = new Dictionary<string, string>(); 453 } 454 foreach (string item in filters) 455 { 456 if (!this.GroupBy.Keys.Contains(item)) 457 { 458 this.GroupBy.Add(item, dataRow[item].ToString()); 459 } 460 461 } 462 } 463 464 if (element[0].Equals("chart") && element.Count() > 5) 465 { 466 this.ColumnNameForHead = element[5].Equals("1"); 467 this.ColumnStart = string.IsNullOrEmpty(element[6]) ? -1 : int.Parse(element[6]); 468 this.ColumnEnd = string.IsNullOrEmpty(element[7]) ? -1 : int.Parse(element[7]); 469 } 470 } 471 } 472 } 473 474 public Word.Range Range { get; set; } 475 public int Level { get; set; } 476 public int TableIndex { get; set; } 477 public string ElementName { get; set; } 478 479 public DataRow DataRow { get; set; } 480 public Dictionary<string, string> GroupBy { get; set; } 481 482 public string Name { get; set; } 483 484 public bool ColumnNameForHead { get; set; } 485 public int ColumnStart { get; set; } 486 public int ColumnEnd { get; set; } 487 488 public string GroupByString 489 { 490 get 491 { 492 if (GroupBy == null || GroupBy.Count == 0) 493 { 494 return string.Empty; 495 } 496 497 string rtn = string.Empty; 498 foreach (string key in this.GroupBy.Keys) 499 { 500 rtn += "and " + key + " = '" + GroupBy[key] + "' "; 501 } 502 return rtn.Substring(3); 503 } 504 } 505 506 public static string GetName(string elementName) 507 { 508 string[] element = elementName.Split('_'); 509 510 511 if (element[0].Equals("label")) 512 { 513 return element[2]; 514 } 515 else 516 { 517 return element[4]; 518 } 519 } 520 } 521 522 /// <summary> 523 /// Table配置项 524 /// </summary> 525 public class TableConfig 526 { 527 public TableConfig(string tableDescr = "") 528 { 529 this.DataRow = 2; 530 this.SummaryRow = -1; 531 532 if (!string.IsNullOrEmpty(tableDescr)) 533 { 534 string[] element = tableDescr.Split(','); 535 foreach (string item in element) 536 { 537 if (!string.IsNullOrEmpty(item)) 538 { 539 string[] configs = item.Split(':'); 540 if (configs.Length == 2) 541 { 542 switch (configs[0].ToLower()) 543 { 544 case "data": 545 case "d": 546 this.DataRow = int.Parse(configs[1]); 547 break; 548 case "summary": 549 case "s": 550 this.SummaryRow = int.Parse(configs[1]); 551 break; 552 case "summaryfilter": 553 case "sf": 554 this.SummaryFilter = configs[1

相关教程