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报表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程