java和C#中快速生成Excel报表
2021/9/6 11:06:51
本文主要是介绍java和C#中快速生成Excel报表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
先看java代码:
try{ XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet xssfSheet = wb.createSheet(); Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(mysqlUrl,"root","root"); Statement statement = conn.createStatement(); String sql = "SELECT * FROM cellday where time >= '" + strartTime + "' and time <= '" + endTime + "' "; log.info("sql------>" + sql); ResultSet rs = statement.executeQuery(sql); ResultSetMetaData metaData = rs.getMetaData(); //获取查询结果所有字段名 int count = metaData.getColumnCount(); XSSFRow row0 = xssfSheet.createRow(0); XSSFCell cell; for(int i = 0;i < count;i++){ cell = row0.createCell(i); cell.setCellValue(metaData.getColumnName(i + 1)); } int rowIndex = 1; while (rs.next()){ XSSFRow row = xssfSheet.createRow(rowIndex++); for(int i = 0;i < count;i++){ cell = row.createCell(i); cell.setCellValue(rs.getString(i + 1)); } } FileOutputStream out = new FileOutputStream(monthReportPath + "\\小计" + endTime.replace("-","") +".xlsx"); wb.write(out); wb.close(); out.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
C#代码:
private void exportRxLevBothDrop_cell(Dictionary<string, Dictionary<string, string>> rxLevBothDrop_cell_Dic, string rxLevBothDrop_cell_path) { Excel.Application application = new Excel.Application(); Workbooks workbooks = application.Workbooks; workbooks.Add(XlSheetType.xlWorksheet); Workbook workbook = application.ActiveWorkbook; Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1); worksheet.Name = "统计"; worksheet.Cells[1, 1] = "小区名"; worksheet.Cells[1, 2] = "lac"; worksheet.Cells[1, 3] = "ci"; worksheet.Cells[1, 4] = "日期"; worksheet.Cells[1, 5] = "发生次数"; worksheet.Cells[1, 6] = "车次号"; for (int i = 0; i < rxLevBothDrop_cell_Dic.Count; i++) { var item = rxLevBothDrop_cell_Dic.ElementAt(i); string itemKey = item.Key; string[] arr = itemKey.Split('_'); Dictionary<String, String> itemValue = item.Value; worksheet.Cells[i + 2, 1] = arr[0]; worksheet.Cells[i + 2, 2] = arr[1]; worksheet.Cells[i + 2, 3] = arr[2]; worksheet.Cells[i + 2, 4] = arr[3]; worksheet.Cells[i + 2, 5] = itemValue["发生次数"]; worksheet.Cells[i + 2, 6] = itemValue["车次号"]; } workbook.SaveAs(rxLevBothDrop_cell_path); workbook.Close(); application.Quit(); MessageBox.Show("统计结果已分析完成,请查看" + rxLevBothDrop_cell_path); }
这篇关于java和C#中快速生成Excel报表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#