XLSBuffConfigLoader.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 
  2. using CommonLang;
  3. using CommonLang.IO;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.XSSF.UserModel;
  7. using System;
  8. using System.IO;
  9. using XmdsCommonServer.XLS.Data;
  10. namespace XmdsCommonServer.XLS
  11. {
  12. public class XLSBuffConfigLoader : XLSLoader
  13. {
  14. private HashMap<int, BuffConfig> BuffDatas = new HashMap<int, BuffConfig>();
  15. public XLSBuffConfigLoader(string path)
  16. {
  17. byte[] data = Resource.LoadData(path);
  18. if (data == null)
  19. {
  20. throw new Exception("Can not read xls file : " + path);
  21. }
  22. using (MemoryStream ms = new MemoryStream(data))
  23. {
  24. IWorkbook Workbook = null;
  25. if (path.Contains(".xlsx"))
  26. {
  27. Workbook = new XSSFWorkbook(ms);
  28. }
  29. else if (path.Contains(".xls"))
  30. {
  31. Workbook = new HSSFWorkbook(ms);
  32. }
  33. for (int si = 0; si < Workbook.NumberOfSheets; si++)
  34. {
  35. ISheet sheet = Workbook.GetSheetAt(si) as ISheet;
  36. try
  37. {
  38. LoadSheet(sheet);
  39. }
  40. catch (Exception error)
  41. {
  42. throw new Exception(string.Format("XLSBuffConfigLoader 初始化Buff配置表SheetName = {0},Error = {1}",
  43. sheet.SheetName, error.ToString()));
  44. }
  45. }
  46. }
  47. log.Info("XLSBuffConfigLoader 初始化Buff配置.");
  48. }
  49. private void LoadSheet(ISheet sheet)
  50. {
  51. try
  52. {
  53. foreach (BuffConfig m in LoadSheet<BuffConfig>(sheet))
  54. {
  55. BuffDatas.Add(m.TriggerID, m);
  56. }
  57. }
  58. catch (Exception error)
  59. {
  60. throw new Exception(string.Format("XLSRandomMonsterSkillLoader Error : index = {0} Error = {1}", 0, error.ToString()));
  61. }
  62. log.Info(string.Format("loadSheet【{0}】Complete", sheet.SheetName));
  63. }
  64. public BuffConfig GetBuffData(int BuffID)
  65. {
  66. return this.BuffDatas.Get(BuffID);
  67. }
  68. public HashMap<int, BuffConfig> GetAllDatas()
  69. {
  70. return this.BuffDatas;
  71. }
  72. }
  73. }