Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using DevExpress.Spreadsheet;
  8.  
  9.  
  10. using System.IO;
  11.  
  12. using DevExpress.Data.Filtering;
  13. using DevExpress.Xpo;
  14. using DevExpress.ExpressApp;
  15. using DevExpress.ExpressApp.Win;
  16. using System.Threading;
  17. using System.Globalization;
  18.  
  19. namespace kashiash.utils
  20. {
  21.  
  22. public interface IImportujeExcela
  23. {
  24. void WczytajWiersz(int i, ExcelImporterHelper importHelper);
  25.  
  26. IObjectSpace ObjectSpace { set; }
  27. }
  28.  
  29. public abstract class ExcelImporter : IImportujeExcela
  30. {
  31. public IObjectSpace objectSpace = null;
  32.  
  33. public IObjectSpace ObjectSpace
  34. {
  35. set
  36. {
  37. objectSpace = value;
  38. }
  39. }
  40.  
  41.  
  42.  
  43. public abstract void WczytajWiersz(int i, ExcelImporterHelper importHelper);
  44.  
  45. }
  46.  
  47.  
  48. public class ExcellImporter : IDisposable
  49. {
  50.  
  51. Workbook workbook = null;
  52. Worksheet worksheet = null;
  53. IObjectSpace objectSpace = null;
  54. IImportujeExcela importer = null;
  55. ExcelImporterHelper importHelper = null;
  56. string IdentifyString = string.Empty;
  57. int startRow = 0;
  58. WinApplication winApplication = null;
  59.  
  60. private readonly string[,] splashUpdates = new string[,]{
  61. { "Prosze czekać...", "Rozpoczynamy..." },
  62. { "A bit more...", "Wczytuję dane..." },
  63. { "Trwa zapisywanie do bazy!", "Kończymy..." } };
  64.  
  65.  
  66.  
  67.  
  68. public ExcellImporter(IObjectSpace os)
  69. {
  70. objectSpace = os;
  71. }
  72.  
  73. public ExcellImporter(IImportujeExcela ie, int headerRow, string identify)
  74. {
  75. IdentifyString = identify;
  76. startRow = headerRow;
  77. importer = ie;
  78. }
  79.  
  80.  
  81. public ExcellImporter(IObjectSpace os, IImportujeExcela ie, string identify)
  82. {
  83. objectSpace = os;
  84. importer = ie;
  85.  
  86. IdentifyString = identify;
  87. }
  88.  
  89. public ExcellImporter(IObjectSpace os, IImportujeExcela ie, WinApplication wap, string identify)
  90. {
  91. objectSpace = os;
  92. importer = ie;
  93.  
  94. IdentifyString = identify;
  95.  
  96. winApplication = wap;
  97. }
  98.  
  99. public ExcellImporter(IObjectSpace os, IImportujeExcela ie, WinApplication wap, int headerRow, string identify)
  100. {
  101. objectSpace = os;
  102. importer = ie;
  103.  
  104. IdentifyString = identify;
  105. startRow = headerRow;
  106. winApplication = wap;
  107.  
  108.  
  109. }
  110. public string[] PobierzPliki()
  111. {
  112.  
  113. OpenFileDialog openFileDialog1 = new OpenFileDialog();
  114.  
  115. openFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|All files (*.*)|*.*";
  116. // openFileDialog1.FilterIndex = 1;
  117. openFileDialog1.RestoreDirectory = true;
  118. openFileDialog1.Multiselect = true;
  119.  
  120. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  121. {
  122.  
  123. return openFileDialog1.FileNames;
  124. }
  125.  
  126. return new string[0];
  127. }
  128.  
  129.  
  130. public void Execute()
  131. {
  132. if (winApplication != null)
  133. {
  134. // Set the WinApplication.SplashScreen property to provide a splash screen for your application.
  135. winApplication.SplashScreen = new DevExpress.ExpressApp.Win.Utils.DXSplashScreen();
  136. // Call the SetDisplayText to provide the initial display text for your splash screen.
  137. winApplication.SplashScreen.SetDisplayText(splashUpdates[0, 0]);
  138. }
  139. string[] pliki = PobierzPliki();
  140.  
  141. foreach (string fileName in pliki)
  142. {
  143. if (winApplication != null)
  144. {
  145. winApplication.StartSplash();
  146. ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[0, 0], splashUpdates[0, 1]);
  147. }
  148. try
  149. {
  150. Importuj(fileName);
  151. }
  152. finally
  153. {
  154. if (winApplication != null)
  155. {
  156. ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[2, 0], splashUpdates[2, 1]);
  157. winApplication.StopSplash();
  158. }
  159. }
  160. }
  161. if (winApplication != null)
  162. {
  163. ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[2, 0], splashUpdates[2, 1]);
  164. winApplication.StopSplash();
  165. }
  166. }
  167.  
  168. public void Execute(string fileName)
  169. {
  170.  
  171. try
  172. {
  173. Importuj(fileName);
  174. }
  175. finally
  176. {
  177.  
  178. }
  179. }
  180.  
  181. private void Importuj(string excelFilePath)
  182. {
  183. if (objectSpace != null)
  184. {
  185. importer.ObjectSpace = objectSpace;
  186. }
  187. if (!File.Exists(excelFilePath)) throw new FileNotFoundException(excelFilePath);
  188.  
  189. using (workbook = new Workbook())
  190. {
  191. workbook.LoadDocument(excelFilePath);
  192. worksheet = workbook.Worksheets[0];
  193. using (importHelper = new ExcelImporterHelper(worksheet))
  194. {
  195. Cell Rodzaj = worksheet.Rows[0]["A"];
  196. if (winApplication != null)
  197. {
  198. ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(excelFilePath, splashUpdates[1, 1]);
  199. }
  200. if (IdentifyString == string.Empty || Rodzaj.Value.ToString().Trim() == IdentifyString)
  201. {
  202. Range usedRange = worksheet.GetUsedRange();
  203. int lk = usedRange.RowCount;
  204. int prog = 30;
  205. for (int i = 1; i < lk; i++)
  206. {
  207. if (i >= startRow)
  208. {
  209. if (winApplication != null)
  210. {
  211. if (i / lk * 100 > prog)
  212. {
  213. prog += 10;
  214. string description = $"{excelFilePath} {prog}%%";
  215. ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(excelFilePath, description);
  216. }
  217. }
  218. importer.WczytajWiersz(i, importHelper);
  219. }
  220. }
  221.  
  222. if (winApplication != null)
  223. {
  224. ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[2, 0], splashUpdates[2, 1]);
  225. }
  226. if (objectSpace != null)
  227. {
  228. objectSpace.CommitChanges();
  229. objectSpace.Refresh();
  230. }
  231. }
  232. }
  233. }
  234. }
  235.  
  236. public void Dispose()
  237. {
  238. //
  239. }
  240. }
  241.  
  242. public class ExcelImporterHelper : IDisposable
  243. {
  244. Worksheet worksheet = null;
  245. public ExcelImporterHelper(Worksheet _worksheet)
  246. {
  247. worksheet = _worksheet;
  248. }
  249.  
  250.  
  251. public DateTime GetCellDateValue(int i, string v)
  252. {
  253. Cell dataCell = worksheet.Rows[i][v];
  254. return kashiash.utils.XUtilConvert.ParsujDate(dataCell.Value.ToString().Trim());
  255. }
  256. public decimal GetCellDecimalValue(int i, string v)
  257. {
  258. Cell decimalCell = worksheet.Rows[i][v];
  259. return System.Convert.ToDecimal(decimalCell.Value.NumericValue);
  260. }
  261.  
  262.  
  263. public decimal GetCellDecimalValue(int i, string v, string culture)
  264. {
  265. Cell decimalCell = worksheet.Rows[i][v];
  266. return decimal.Parse(decimalCell.Value.ToString().Trim(), new CultureInfo(culture));
  267. }
  268. public string GetCellStringValue(int i, string v)
  269. {
  270. Cell stringCell = worksheet.Rows[i][v];
  271. return stringCell.Value.ToString().Trim();
  272. }
  273. public int GetCellIntValue(int i, string v)
  274. {
  275. Cell cell = worksheet.Rows[i][v];
  276. return kashiash.utils.XUtilConvert.ObjectToInt(cell.Value.ToString().Trim());
  277. }
  278.  
  279. public void Dispose()
  280. {
  281. //this.Dispose();
  282. }
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement