VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C#封装的VSTO Excel操作类(2)

else 309 { 310 Console.WriteLine("Error source_rows length not is new_rows length!"); 311 } 312 } 313 314 /// <summary> 315 /// 复制表头到另一个sheet中 316 /// </summary> 317 /// <param name="sourceWorksheet">表头所在的sheet</param> 318 /// <param name="newWorksheet">要复制到的sheet</param> 319 /// <param name="start">起始位置</param> 320 public void CopyHeader(Worksheet sourceWorksheet, Worksheet newWorksheet, int start = 1) 321 { 322 if (sourceWorksheet.Rows != null) 323 sourceWorksheet.Rows[start].Copy(newWorksheet.Cells[1, 1]); //把数据表的表头复制到新表中 324 } 325 326 /// <summary> 327 /// 设置特定列的数据 328 /// </summary> 329 /// <param name="worksheet">源表</param> 330 /// <param name="row">要设置的列号</param> 331 /// <param name="len">长度</param> 332 /// <param name="value">要设的值</param> 333 /// /// 334 public void SetSheetRow(Worksheet worksheet, int row, int len, string value) 335 { 336 //int intrngEnd = this.GetEndRow(worksheet);//取特定列最后一列的长度 337 worksheet.Cells[65536, row].End[XlDirection.xlUp].Offset(1, 0).Resize(len, 1).Value = value; 338 } 339 340 /// <summary> 341 /// 取有效列的最后一列的长度 342 /// </summary> 343 /// <param name="worksheet"></param> 344 /// <returns></returns> 345 public int GetEndRow(Worksheet worksheet) 346 { 347 int res = worksheet.UsedRange.Rows.Count; 348 return res; 349 } 350 351 /// <summary> 352 /// 插入图片 353 /// </summary> 354 /// <param name="path">图片路径</param> 355 /// <param name="worksheet">要插入的表</param> 356 /// <param name="range">要插入的range</param> 357 public void AddPic(string path, Worksheet worksheet, Range range) 358 { 359 this.AddPic(path, worksheet, range, range.Width, range.Height); 360 } 361 362 /// <summary> 363 /// 插入图片 364 /// </summary> 365 /// <param name="path">图片路径</param> 366 /// <param name="worksheet">要插入的表</param> 367 /// <param name="range">要插入的range</param> 368 /// <param name="width">图片的宽度</param> 369 /// <param name="height">图片的高度</param> 370 public void AddPic(string path, Worksheet worksheet, Range range, int width, int height) 371 { 372 worksheet.Shapes.AddPicture(path, Microsoft.Office.Core.MsoTriState.msoCTrue, 373 Microsoft.Office.Core.MsoTriState.msoCTrue, range.Left, range.Top, width, height).Placement = 374 XlPlacement.xlMoveAndSize; 375 } 376 377 /// <summary> 378 /// 批量插入图片 379 /// </summary> 380 /// <param name="pngdic">单元格范围-图片名</param> 381 /// <param name="imgBase">图片根目录</param> 382 /// <param name="worksheet">要插入图片的worksheet</param> 383 /// <returns>返回处理好的图片日志</returns> 384 public string InsertMultipleImages(Dictionary<string, string> pngdic, string imgBase, Worksheet worksheet) 385 { 386 string msg = null; 387 foreach (var s in pngdic) 388 { 389 string imgPath = Path.Combine(imgBase, s.Value); 390 if (!Exists(imgPath)) 391 { 392 continue; 393 } 394 395 Range range = worksheet.Range[s.Key]; 396 AddPic(imgPath, worksheet, range); 397 msg = s.Value + "\t" + s.Key + "\t\t\t" + range.Left.ToString() + "\t" + range.Top.ToString() + "\n"; 398 } 399 400 return msg; 401 } 402 403 /// <summary> 404 /// 主要实现这个方法 405 /// </summary> 406 /// <param name="path">要打开的文件路径</param> 407 public abstract void Handler(string path = null); 408 /// <summary> 409 /// 开启或者关闭屏幕刷新 410 /// </summary> 411 public bool ScreenUpdating 412 { 413 get => ExcelApp.ScreenUpdating; 414 set => ExcelApp.ScreenUpdating = value; 415 } 416 /// <summary> 417 /// Excel可见性 418 /// </summary> 419 public bool Visible 420 { 421 get => ExcelApp.Visible; 422 set => ExcelApp.Visible = value; 423 } 424 /// <summary> 425 /// 是否显示警告窗体 426 /// </summary> 427 public bool DisplayAlerts 428 { 429 get => ExcelApp.DisplayAlerts; 430 set => ExcelApp.DisplayAlerts = value; 431 } 432 private bool _debugMode; 433 /// <summary> 434 /// 设置DEBUG模式 435 /// </summary> 436 public bool DebugMode 437 { 438 get => _debugMode; 439 set 440 { 441 _debugMode = value; 442 //设置是否显示警告窗体 443 DisplayAlerts = value; 444 //设置是否显示Excel 445 Visible = value; 446 //禁止刷新屏幕 447 ScreenUpdating = value; 448 } 449 } 450 } 451 /// <summary> 452 /// 关闭Excel进程 453 /// </summary> 454 public class KeyMyExcelProcess 455 { 456 [DllImport("User32.dll", CharSet = CharSet.Auto)] 457 public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id); 458 public static void Kill(Application excel) 459 { 460 try 461 { 462 IntPtr t = new IntPtr(excel.Hwnd); //得到这个句柄,具体作用是得到这块内存入口 463 GetWindowThreadProcessId(t, out var k); //得到本进程唯一标志k 464 System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到对进程k的引用 465 p.Kill(); //关闭进程k 466 } 467 catch (Exception e) 468 { 469 Console.WriteLine(e.Message); 470 } 471 472 } 473 } 474 }
复制代码

 

最后放上github的地址

https://github.com/tchivs/ExcelLib


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号