ExportUtil.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using DevComponents.DotNetBar.SuperGrid;
  9. namespace WCS_Client.UC
  10. {
  11. public class ExportUtil
  12. {
  13. public static string ShowSaveFileDialog()
  14. {
  15. try
  16. {
  17. //对话框
  18. SaveFileDialog Sfd = new SaveFileDialog();
  19. Sfd.Title = "导出数据";
  20. Sfd.Filter = "EXECL文件(*.xls) |*.xls|EXECL文件(*.xlsx) |*.xlsx|文本文件(*.txt)|*.txt";
  21. Sfd.FilterIndex = 0;
  22. if (Sfd.ShowDialog() == DialogResult.OK)
  23. {
  24. return Sfd.FileName;
  25. }
  26. }
  27. catch
  28. {
  29. }
  30. return "";
  31. }
  32. public static void Execute(string PathFileName, DataTable dt, List<GridColumn> gcList)
  33. {
  34. try
  35. {
  36. if (gcList == null || gcList.Count <= 0)
  37. {
  38. return;
  39. }
  40. if (dt == null)
  41. {
  42. dt = new DataTable();
  43. }
  44. if (!string.IsNullOrEmpty(PathFileName))
  45. {
  46. FileInfo fi = new FileInfo(PathFileName);
  47. if (!Directory.Exists(fi.DirectoryName))
  48. {
  49. Directory.CreateDirectory(fi.DirectoryName);
  50. }
  51. if (File.Exists(PathFileName))
  52. {
  53. File.Delete(PathFileName);
  54. }
  55. FileStream fs = new FileStream(PathFileName, FileMode.Create, FileAccess.Write);
  56. if (fi.Extension.ToUpper() == ".XLS" || fi.Extension.ToUpper() == ".XLS")
  57. {
  58. Export_Excel(fs, dt, gcList);
  59. }
  60. else if (fi.Extension.ToUpper() == ".TXT")
  61. {
  62. Export_Txt(fs, dt, gcList);
  63. }
  64. }
  65. }
  66. catch
  67. {
  68. }
  69. }
  70. private static void Export_Excel(FileStream fs, DataTable dt, List<GridColumn> gcList)
  71. {
  72. try
  73. {
  74. //创建Excel文件的对象
  75. NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
  76. NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1"); //添加一个sheet
  77. int GCCount = gcList.Count;
  78. NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
  79. for (int i = 0; i < GCCount; i++)
  80. {
  81. string ColName = gcList[i].DataPropertyName;
  82. if (!dt.Columns.Contains(ColName))
  83. {
  84. dt.Columns.Add(ColName, typeof(string));
  85. }
  86. row1.CreateCell(i).SetCellValue(gcList[i].HeaderText);
  87. }
  88. int r = 1;
  89. foreach (DataRow dr in dt.Rows)
  90. {
  91. NPOI.SS.UserModel.IRow erow = sheet1.CreateRow(r);
  92. for (int i = 0; i < GCCount; i++)
  93. {
  94. object o = dr[gcList[i].DataPropertyName];
  95. erow.CreateCell(i).SetCellValue(o != null ? o.ToString() : "");
  96. }
  97. r++;
  98. }
  99. book.Write(fs);
  100. book = null;
  101. }
  102. catch
  103. {
  104. }
  105. finally
  106. {
  107. if (fs != null)
  108. {
  109. fs.Close();
  110. fs.Dispose();
  111. }
  112. }
  113. }
  114. private static void Export_Txt(FileStream fs, DataTable dt, List<GridColumn> gcList)
  115. {
  116. try
  117. {
  118. //创建Excel文件的对象
  119. StreamWriter strwriter = new StreamWriter(fs);
  120. string s = "";
  121. bool IsTag = true;
  122. foreach (GridColumn gc in gcList)
  123. {
  124. string ColName = gc.DataPropertyName;
  125. if (!dt.Columns.Contains(ColName))
  126. {
  127. dt.Columns.Add(ColName, typeof(string));
  128. }
  129. if (IsTag)
  130. {
  131. IsTag = false;
  132. s = gc.HeaderText;
  133. }
  134. else
  135. {
  136. s = s + "," + gc.HeaderText;
  137. }
  138. }
  139. strwriter.WriteLine(s);
  140. foreach (DataRow dr in dt.Rows)
  141. {
  142. s = "";
  143. IsTag = true;
  144. foreach (GridColumn gc in gcList)
  145. {
  146. object o = dr[gc.DataPropertyName];
  147. string k = (o != null ? o.ToString() : "");
  148. if (IsTag)
  149. {
  150. IsTag = false;
  151. s = k;
  152. }
  153. else
  154. {
  155. s = s + "," + k;
  156. }
  157. }
  158. strwriter.WriteLine(s);
  159. }
  160. strwriter.Flush();
  161. strwriter.Close();
  162. strwriter.Dispose();
  163. }
  164. catch
  165. {
  166. }
  167. finally
  168. {
  169. fs.Close();
  170. fs.Dispose();
  171. }
  172. }
  173. }
  174. }