spring boot要怎么接收json对象
更新时间:2023-09-06 15:09
最新回答
代码实例: package edu.sjtu.erplab.poi; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstream; import java.text.simpledateformat; import java.util.date; import java.util.hashmap; import java.util.map; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfdateutil; import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.poifs.filesystem.poifsfilesystem; /** * 操作excel表格的功能类 */ public class excelreader { private poifsfilesystem fs; private hssfworkbook wb; private hssfsheet sheet; private hssfrow row; /** * 读取excel表格表头的内容 * @param inputstream * @return string 表头内容的数组 */ public string[] readexceltitle(inputstream is) { try { fs = new poifsfilesystem(is); wb = new hssfworkbook(fs); } catch (ioexception e) { e.printstacktrace(); } sheet = wb.getsheetat(0); row = sheet.getrow(0); // 标题总列数 int colnum = row.getphysicalnumberofcells(); system.out.println("colnum:" + colnum); string[] title = new string[colnum]; for (int i = 0; i < colnum; i++) { //title[i] = getstringcellvalue(row.getcell((short) i)); title[i] = getcellformatvalue(row.getcell((short) i)); } return title; } /** * 读取excel数据内容 * @param inputstream * @return map 包含单元格数据内容的map对象 */ public map readexcelcontent(inputstream is) { map content = new hashmap(); string str = ""; try { fs = new poifsfilesystem(is); wb = new hssfworkbook(fs); } catch (ioexception e) { e.printstacktrace(); } sheet = wb.getsheetat(0); // 得到总行数 int rownum = sheet.getlastrownum(); row = sheet.getrow(0); int colnum = row.getphysicalnumberofcells(); // 正文内容应该从第二行开始,第一行为表头的标题 for (int i = 1; i <= rownum; i++) { row = sheet.getrow(i); int j = 0; while (j < colnum) { // 每个单元格的数据内容用"-"分割开,以后需要时用string类的replace()方法还原数据 // 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean // str += getstringcellvalue(row.getcell((short) j)).trim() + // "-"; str += getcellformatvalue(row.getcell((short) j)).trim() + " "; j++; } content.put(i, str); str = ""; } return content; } /** * 获取单元格数据内容为字符串类型的数据 * * @param cell excel单元格 * @return string 单元格数据内容 */ private string getstringcellvalue(hssfcell cell) { string strcell = ""; switch (cell.getcelltype()) { case hssfcell.cell_type_string: strcell = cell.getstringcellvalue(); break; case hssfcell.cell_type_numeric: strcell = string.valueof(cell.getnumericcellvalue()); break; case hssfcell.cell_type_boolean: strcell = string.valueof(cell.getbooleancellvalue()); break; case hssfcell.cell_type_blank: strcell = ""; break; default: strcell = ""; break; } if (strcell.equals("") || strcell == null) { return ""; } if (cell == null) { return ""; } return strcell; } /** * 获取单元格数据内容为日期类型的数据 * * @param cell * excel单元格 * @return string 单元格数据内容 */ private string getdatecellvalue(hssfcell cell) { string result = ""; try { int celltype = cell.getcelltype(); if (celltype == hssfcell.cell_type_numeric) { date date = cell.getdatecellvalue(); result = (date.getyear() + 1900) + "-" + (date.getmonth() + 1) + "-" + date.getdate(); } else if (celltype == hssfcell.cell_type_string) { string date = getstringcellvalue(cell); result = date.replaceall("[年月]", "-").replace("日", "").trim(); } else if (celltype == hssfcell.cell_type_blank) { result = ""; } } catch (exception e) { system.out.println("日期格式不正确!"); e.printstacktrace(); } return result; } /** * 根据hssfcell类型设置数据 * @param cell * @return */ private string getcellformatvalue(hssfcell cell) { string cellvalue = ""; if (cell != null) { // 判断当前cell的type switch (cell.getcelltype()) { // 如果当前cell的type为numeric case hssfcell.cell_type_numeric: case hssfcell.cell_type_formula: { // 判断当前的cell是否为date if (hssfdateutil.iscelldateformatted(cell)) { // 如果是date类型则,转化为data格式 //方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00 //cellvalue = cell.getdatecellvalue().tolocalestring(); //方法2:这样子的data格式是不带带时分秒的:2011-10-12 date date = cell.getdatecellvalue(); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); cellvalue = sdf.format(date); } // 如果是纯数字 else { // 取得当前cell的数值 cellvalue = string.valueof(cell.getnumericcellvalue()); } break; } // 如果当前cell的type为strin case hssfcell.cell_type_string: // 取得当前的cell字符串 cellvalue = cell.getrichstringcellvalue().getstring(); break; // 默认的cell值 default: cellvalue = " "; } } else { cellvalue = ""; } return cellvalue; } public static void main(string[] args) { try { // 对读取excel表格标题测试 inputstream is = new fileinputstream("d:\\test2.xls"); excelreader excelreader = new excelreader(); string[] title = excelreader.readexceltitle(is); system.out.println("获得excel表格的标题:"); for (string s : title) { system.out.print(s + " "); } // 对读取excel表格内容测试 inputstream is2 = new fileinputstream("d:\\test2.xls"); map map = excelreader.readexcelcontent(is2); system.out.println("获得excel表格的内容:"); for (int i = 1; i <= map.size(); i++) { system.out.println(map.get(i)); } } catch (filenotfoundexception e) { system.out.println("未找到指定路径的文件!"); e.printstacktrace(); } } } 3.总结 因为excel单元格中的内容往往都有一定的格式,比如日期型,数字型,字符串型,因此在读取的时候要进行格式判断,不然会出现错误。常见的就是不能正常读取日期。在代码实例中有一个方法: getcellformatvalue(hssfcell cell) 往这个方法中传入excel单元格就能识别单元格格式,并转化为正确的格式。 ps:2012-2-23 代码实例中有一段代码: int colnum = row.getphysicalnumberofcells(); 其中的hssfrow.getphysicalnumberofcells();这个方法是用于获取一行中存在的单元格数,poi的官方api中有给出getphysicalnumberofcells方法的解释
String excelDir ="c:/tep.xls"; //excel路径 FileInputStream finput = new FileInputStream("excelDir" ); POIFSFileSystem fs = new POIFSFileSystem( finput ); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); //参数 0 代表第一个sheet HSSFRow row = sheet.getRow((short)0); //得到第一行 HSSFCell cell = row .getCell( (short) 0); //得到第一列 System.out.println(cell.getNumericCellValue()); //得到第一行第一列的单元格内容. 分数是数字型,注意内容的类型. 获取颜色好像没办法
相关问答
更多-
怎么用java读取Excel单元格属性[2022-01-04]
poi好像不能读取超过128列的数据,不知道是不是我记错了一直用的jxl,但是网上和官方发布的jxl有个致命缺陷,是编码问题,需要自己更改代码,再重编译jxl包,我建议你用poi吧,一般poi编码不会有问题的。 言归正传jxl设计的时候不是将颜色等信息直接设计到单元格Cell类中,而是通过一个CellFormat存储各个属性 Workbook book = Workbook.getWorkbook(new File("fileName")); 读取你的文件Sheet sheet = book.getShee ... -
用java编程获取excel单元格[2022-02-27]
也不是没有办法的,POI或JXL这些组件也是用纯JAVA来写的,不依靠它们相当于用JAVA来实现它们的部份功能。具体办法得研究一下他们的源码才能知道。 -
java poi怎么获取excel单元格的内容[2021-05-12]
package edu.sjtu.erplab.poi; import java.io.InputStream&ch=ww.xqy.chain" target="_blank" class="link-baike">FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; im ... -
您必须提供双倍的价值。 正如文件所说 : setCellValue public void setCellValue(double value) 为单元格设置一个数值 参数 : 值 - 将此单元格设置为的数值。 对于公式,我们将设置预先计算的值,对于数字我们将设置其值。 对于其他类型,我们将把单元格更改为数字单元格并设置其值。 所以,它应该是 dCell.setCellValue(new Double("123")); 要么 dCell.setCellValue(123); //Remember, th ...
-
使用Apache POI从Excel中的空白单元读取颜色到Java(Read a Color from a Blank Cell in Excel to Java using Apache POI)[2022-08-06]
一个单色的单元格永远不能为空。 它必须存在。 所以我们只能循环现有的单元格。 每个定义的CellStyle不为空。 但是如果没有颜色, CellStyle.getFillForegroundColorColor可以返回null。 所以我们需要检查。 假设下面的表格: 码: import org.apache.poi.ss.usermodel.*; import java.io.*; import java.util.Arrays; class ReadColorsFromExcel { public ... -
使用Apache POI在Excel中的一定数量的单元格后字体消失(Font disappears after a certain amount of cells in Excel using Apache POI)[2023-09-02]
Apache POI限制为每个工作簿32767个字体 您需要找到一种重用字体和单元格样式的方法。 Apache POI is limited to 32767 Fonts per workbook You will need to find a way to reuse the fonts and cell styles. -
我将此作为unicode粘贴到xls文件的单元格A1:
This is a test. Will this text be bold or italic
这个html行产生了这个: 这是一个测试。 这个文本是粗体还是斜体 我的代码: public class ExcelWithHtml { //This is a test. Will this text be bold or // ...
-
打开xlsx文件选择列,右键单击单元格 - >格式化单元格...-> custom-> type = 0.00000000000单击确定。 现在无论你在那个单元格上写什么,它都将以该格式打印,如果你选择它也会显示总和。 按代码 import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...
-
使用Java中的POI获取Excel单元格背景颜色不匹配(Getting Excel cell background color using POI in Java doesn´t match)[2024-01-04]
我认为由于Excel 2003调色板仅包含56种颜色,因此无法获得单元格的确切颜色。 我发现获得确切颜色的独特方式是,当您在Excel的选项中将Excel格式保存为.xls,然后在保存菜单中,我们可以更改所需颜色的调色板颜色。 I think that it isn´t possible to obtain the exact color of the cell because of Excel 2003 palette contains only 56 colors. The unique way tha ... -
您可以使用CellReference来解析所需的String,这是一个示例: String[] cellStrings = "A2:A8".split(":"); CellReference start = new CellReference(cellStrings[0]); CellReference end = new CellReference(cellStrings[1]); CellRangeAddress address = new CellRangeAddre ...