先看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);
}