Form4.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using Aspose.Cells;
  2. using Aspose.Cells.Drawing;
  3. using Aspose.Cells.Rendering;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.XSSF.UserModel;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.Drawing.Imaging;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Threading;
  19. using System.Windows.Forms;
  20. using UMESDLLService;
  21. namespace FileWatcher
  22. {
  23. public partial class Form4 : Form
  24. {
  25. public Form4()
  26. {
  27. InitializeComponent();
  28. }
  29. private void label12_Click(object sender, EventArgs e)
  30. {
  31. }
  32. private void textBox7_TextChanged(object sender, EventArgs e)
  33. {
  34. }
  35. private void textBox3_TextChanged(object sender, EventArgs e)
  36. {
  37. }
  38. Thread InitDB;
  39. Thread InitDB1;
  40. Thread InitDB2;
  41. Thread InitDB3;
  42. DataTable dt;
  43. //DataHelper dh = new DataHelper();
  44. public void ConvertExcelToImages(string excelFilePath, string outputFolder)
  45. {
  46. // 确保输出目录存在
  47. if (!Directory.Exists(outputFolder))
  48. {
  49. Directory.CreateDirectory(outputFolder);
  50. }
  51. // 加载Excel工作簿
  52. Workbook workbook = new Workbook(excelFilePath);
  53. // 处理每个工作表
  54. foreach (Worksheet worksheet in workbook.Worksheets)
  55. {
  56. ProcessWorksheet(worksheet, outputFolder);
  57. }
  58. }
  59. private void ProcessWorksheet(Worksheet worksheet, string outputFolder)
  60. {
  61. // 获取工作表中的所有图片
  62. List<Picture> pictures = new List<Picture>();
  63. foreach (Picture picture in worksheet.Pictures)
  64. {
  65. pictures.Add(picture);
  66. }
  67. // 获取使用的行数
  68. int totalRows = worksheet.Cells.MaxDataRow + 1;
  69. int imageIndex = 1;
  70. // 每30行生成一张图片
  71. for (int startRow = 0; startRow < totalRows; startRow += 30)
  72. {
  73. int endRow = Math.Min(startRow + 29, totalRows - 1);
  74. // 创建图片选项
  75. ImageOrPrintOptions options = new ImageOrPrintOptions();
  76. options.ImageFormat = ImageFormat.Png;
  77. options.OnePagePerSheet = false;
  78. options.PrintingPage = PrintingPageType.IgnoreBlank;
  79. worksheet.PageSetup.PrintArea = $"A{startRow + 1}:AC{endRow + 1}";
  80. // 设置只渲染指定的行范围
  81. // 创建SheetRender对象
  82. SheetRender sr = new SheetRender(worksheet, options);
  83. // 获取渲染后的图像
  84. Bitmap bitmap = sr.ToImage(0);
  85. // 如果有图片在当前行范围内,将它们添加到最终图像中
  86. if (pictures.Count > 0)
  87. {
  88. using (Bitmap finalImage = new Bitmap(bitmap.Width+100, bitmap.Height ))
  89. using (Graphics g = Graphics.FromImage(finalImage))
  90. {
  91. // 绘制Excel内容
  92. g.DrawImage(bitmap, 0, 0);
  93. // 绘制原始Excel中的图片
  94. int yOffset = bitmap.Height;
  95. // 保存图像
  96. string imagePath = Path.Combine(outputFolder, $"{worksheet.Name}_{imageIndex}.png");
  97. finalImage.Save(imagePath, ImageFormat.Png);
  98. Console.WriteLine($"Saved: {imagePath}");
  99. }
  100. }
  101. else
  102. {
  103. // 如果没有图片,直接保存渲染的图像
  104. string imagePath = Path.Combine(outputFolder, $"{worksheet.Name}_{imageIndex}.png");
  105. bitmap.Save(imagePath, ImageFormat.Png);
  106. Console.WriteLine($"Saved: {imagePath}");
  107. }
  108. imageIndex++;
  109. }
  110. }
  111. private void Form4_Load(object sender, EventArgs e)
  112. {
  113. string excelFilePath = @"C:\Users\callm\Desktop\新建文件夹 (4)\P802 作业指导书 .xls";
  114. string outputFolder = @"C:\output\images";
  115. ConvertExcelToImages(excelFilePath, outputFolder);
  116. //Console.WriteLine("转换完成。按任意键退出。");
  117. //Console.ReadKey();
  118. }
  119. private void ConnectDB()
  120. {
  121. DataHelper dh = new DataHelper();
  122. for (int i = 150000; i <= 200000; i++)
  123. {
  124. Console.WriteLine(i);
  125. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  126. dh.ExecuteSql(sql, "insert");
  127. }
  128. }
  129. private void ConnectDB1()
  130. {
  131. DataHelper dh = new DataHelper();
  132. for (int i = 350000; i <= 400000; i++)
  133. {
  134. Console.WriteLine(i);
  135. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  136. dh.ExecuteSql(sql, "insert");
  137. }
  138. }
  139. private void ConnectDB2()
  140. {
  141. DataHelper dh = new DataHelper();
  142. for (int i = 550000; i <= 600000; i++)
  143. {
  144. Console.WriteLine(i);
  145. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  146. dh.ExecuteSql(sql, "insert");
  147. }
  148. }
  149. private void ConnectDB3()
  150. {
  151. DataHelper dh = new DataHelper();
  152. for (int i = 650000; i <= 700000; i++)
  153. {
  154. Console.WriteLine( i);
  155. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  156. dh.ExecuteSql(sql, "insert");
  157. }
  158. }
  159. public static string ToUrlEncode(string strCode)
  160. {
  161. StringBuilder sb = new StringBuilder();
  162. byte[] byStr = System.Text.Encoding.UTF8.GetBytes(strCode); //默认是System.Text.Encoding.Default.GetBytes(str)
  163. System.Text.RegularExpressions.Regex regKey = new System.Text.RegularExpressions.Regex("^[A-Za-z0-9]+$");
  164. for (int i = 0; i < byStr.Length; i++)
  165. {
  166. string strBy = Convert.ToChar(byStr[i]).ToString();
  167. if (regKey.IsMatch(strBy))
  168. {
  169. //是字母或者数字则不进行转换
  170. sb.Append(strBy);
  171. }
  172. else
  173. {
  174. sb.Append(@"%" + Convert.ToString(byStr[i], 16));
  175. }
  176. }
  177. return (sb.ToString());
  178. }
  179. private void textBox4_TextChanged(object sender, EventArgs e)
  180. {
  181. }
  182. private void SaveFileToJPG(string filename)
  183. {
  184. DataHelper dh = new DataHelper();
  185. DataTable dt = ExcelToDataTable(@"C:\Users\callm\Desktop\Autosave\Vortex-ICC ID数据库.xlsx", true);
  186. for (int i = 0; i < dt.Rows.Count; i++)
  187. {
  188. string sql = "insert into simlist(si_id,si_code,si_sim)values(simlist_seq.nextval,'SIM-01','" + dt.Rows[i]["ID1"].ToString() + "')";
  189. dh.ExecuteSql(sql, "insert");
  190. }
  191. }
  192. public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
  193. {
  194. DataTable dataTable = null;
  195. FileStream fs = null;
  196. DataColumn column = null;
  197. DataRow dataRow = null;
  198. IWorkbook workbook = null;
  199. ISheet sheet = null;
  200. IRow row = null;
  201. ICell cell = null;
  202. int startRow = 0;
  203. try
  204. {
  205. using (fs = File.OpenRead(filePath))
  206. {
  207. // 2007版本
  208. if (filePath.IndexOf(".xlsx") > 0)
  209. {
  210. workbook = new XSSFWorkbook(fs);
  211. }
  212. // 2003版本
  213. else if (filePath.IndexOf(".xls") > 0)
  214. {
  215. workbook = new HSSFWorkbook(fs);
  216. }
  217. if (workbook != null)
  218. {
  219. sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
  220. dataTable = new DataTable();
  221. if (sheet != null)
  222. {
  223. int rowCount = sheet.LastRowNum;//总行数
  224. if (rowCount > 0)
  225. {
  226. IRow firstRow = sheet.GetRow(0);//第一行
  227. int cellCount = firstRow.LastCellNum;//列数
  228. //构建datatable的列
  229. if (isColumnName)
  230. {
  231. startRow = 1;//如果第一行是列名,则从第二行开始读取
  232. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  233. {
  234. cell = firstRow.GetCell(i);
  235. if (cell != null)
  236. {
  237. if (cell.StringCellValue != null)
  238. {
  239. column = new DataColumn(cell.StringCellValue);
  240. dataTable.Columns.Add(column);
  241. }
  242. }
  243. }
  244. }
  245. else
  246. {
  247. for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
  248. {
  249. column = new DataColumn("column" + (i + 1));
  250. dataTable.Columns.Add(column);
  251. }
  252. }
  253. //填充行
  254. for (int i = startRow; i <= rowCount; ++i)
  255. {
  256. row = sheet.GetRow(i);
  257. if (row == null) continue;
  258. dataRow = dataTable.NewRow();
  259. for (int j = row.FirstCellNum; j < cellCount; ++j)
  260. {
  261. cell = row.GetCell(j);
  262. if (cell == null)
  263. {
  264. dataRow[j] = "";
  265. }
  266. else
  267. {
  268. //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
  269. switch (cell.CellType)
  270. {
  271. case CellType.Blank:
  272. dataRow[j] = "";
  273. break;
  274. case CellType.Numeric:
  275. short format = cell.CellStyle.DataFormat;
  276. //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
  277. if (format == 14 || format == 31 || format == 57 || format == 58)
  278. dataRow[j] = cell.DateCellValue;
  279. else
  280. dataRow[j] = cell.NumericCellValue;
  281. break;
  282. case CellType.String:
  283. dataRow[j] = cell.StringCellValue;
  284. break;
  285. case CellType.Formula:
  286. dataRow[j] = cell.StringCellValue;
  287. break;
  288. }
  289. }
  290. }
  291. dataTable.Rows.Add(dataRow);
  292. }
  293. }
  294. }
  295. }
  296. }
  297. return dataTable;
  298. }
  299. catch (Exception ex)
  300. {
  301. Console.WriteLine(ex.Message);
  302. if (fs != null)
  303. {
  304. fs.Close();
  305. }
  306. return null;
  307. }
  308. }
  309. }
  310. }