else if (value instanceof List<?>) {
时间: 2024-06-01 07:14:01 浏览: 119
This code checks if the value is an instance of the List class with any type of generic parameter. It is similar to checking if the value is a List object, but this check allows for a List with any type of elements.
The else if statement suggests that there is an if statement that checks for another type of value before this one. If that first if statement fails, then this else if statement will be executed.
相关问题
rivate static void export(HttpServletResponse response, File file, String fileName, if (response != null) { // 前端导出 try { write(response, book, fileName); } catch (IOException e) { e.printStackTrace(); } } else { // 本地导出 FileOutputStream fos; try { fos = new FileOutputStream(file); ByteArrayOutputStream ops = new ByteArrayOutputStream(); book.write(ops); fos.write(ops.toByteArray()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) { // 整个 Excel 表格 book 对象 SXSSFWorkbook book = new SXSSFWorkbook(); // 每个 Sheet 页 Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet(); for (Entry<String, List<List<Object>>> entry : entries) { List<List<Object>> sheetDataList = entry.getValue(); Sheet sheet = book.createSheet(entry.getKey()); Drawing<?> patriarch = sheet.createDrawingPatriarch();
这段代码是一个Excel导出的实现,其中包含了两种导出方式:前端导出和本地导出。具体实现如下:
1. 首先创建一个SXSSFWorkbook对象,表示整个Excel表格。
2. 遍历sheetMap中的每个Sheet页,创建对应的Sheet对象,并将Sheet页的名称设置为entry.getKey()。
3. 对于每个Sheet页,遍历其对应的数据列表sheetDataList,创建对应的行和单元格,并将数据填充到单元格中。
4. 对于每个Sheet页,如果有需要下拉选择的单元格,将其加入到数据验证中。
5. 如果response不为空,则表示前端导出,将book写入response流中。否则,表示本地导出,将book写入文件中。
下面是代码的具体实现:
```java
private static void export(HttpServletResponse response, File file, String fileName,
Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) {
// 整个 Excel 表格 book 对象
SXSSFWorkbook book = new SXSSFWorkbook();
// 每个 Sheet 页
Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet();
for (Entry<String, List<List<Object>>> entry : entries) {
List<List<Object>> sheetDataList = entry.getValue();
Sheet sheet = book.createSheet(entry.getKey());
Drawing<?> patriarch = sheet.createDrawingPatriarch();
// 遍历每一行数据
for (int i = 0; i < sheetDataList.size(); i++) {
Row row = sheet.createRow(i);
List<Object> rowDataList = sheetDataList.get(i);
// 遍历每个单元格
for (int j = 0; j < rowDataList.size(); j++) {
Cell cell = row.createCell(j);
Object cellData = rowDataList.get(j);
// 判断单元格数据类型,并设置单元格格式
if (cellData instanceof String) {
cell.setCellValue((String) cellData);
} else if (cellData instanceof Number) {
cell.setCellValue(((Number) cellData).doubleValue());
} else if (cellData instanceof Date) {
cell.setCellValue((Date) cellData);
CellStyle style = book.createCellStyle();
style.setDataFormat(book.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
cell.setCellStyle(style);
} else if (cellData instanceof Calendar) {
cell.setCellValue((Calendar) cellData);
CellStyle style = book.createCellStyle();
style.setDataFormat(book.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
cell.setCellStyle(style);
} else if (cellData instanceof Boolean) {
cell.setCellValue((Boolean) cellData);
} else if (cellData == null) {
cell.setCellValue("");
}
// 设置下拉选择
if (selectMap.containsKey(j)) {
List<String> selectList = selectMap.get(j);
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(selectList.toArray(new String[0]));
CellRangeAddressList addressList = new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex());
DataValidation validation = helper.createValidation(constraint, addressList);
sheet.addValidationData(validation);
}
}
}
}
// 导出Excel
if (response != null) {
// 前端导出
try {
write(response, book, fileName);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// 本地导出
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ByteArrayOutputStream ops = new ByteArrayOutputStream();
book.write(ops);
fos.write(ops.toByteArray());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
其中,write()方法用于将book写入response流中,实现前端导出。具体实现如下:
```java
private static void write(HttpServletResponse response, SXSSFWorkbook book, String fileName) throws IOException {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream os = response.getOutputStream();
book.write(os);
os.flush();
os.close();
}
```
java代码中如何将 {"key1":"value1","key2":"value2","key5":"value5","userDataList2":[{"key6":"value6","userDataList3":[{"key9":"value9","key8":"value8"},{"key9":"value9","key8":"value8"}],"key7":"value7"},{"key6":"value6","userDataList4":[{"key9":"value9","key8":"value8"},{"key9":"value9","key8":"value8"}],"key7":"value7"}],"userDataList1":[{"key3":"value3","key4":"value4"}]} 转换成 <?xml version="1.0" encoding="GBK"?><stream><key1>value1</key1><key2>value2</key2><list name="userDataList1"><row><key3>value3</key3><key4>value4</key4></row></list><list name="userDataList2"><row><key6>value6</key6><key7>value7</key7><list name="userDataList3"><row><key8>value8</key8><key9>value9</key9></row><row><key8>value8</key8><key9>value9</key9></row></list></row><row><key6>value6</key6><key7>value7</key7><list name="userDataList4"><row><key8>value8</key8><key9>value9</key9></row><row><key8>value8</key8><key9>value9</key9></row></list></row></list><key5>value5</key5></stream> XML格式
可以使用Java中的JSONObject和XMLStreamWriter来实现:
```java
import java.io.StringWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonToXmlConverter {
public static String convert(String jsonStr) throws JSONException, XMLStreamException {
JSONObject jsonObj = new JSONObject(jsonStr);
StringWriter sw = new StringWriter();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
writer.writeStartDocument("GBK", "1.0");
writer.writeStartElement("stream");
writeJsonToXml(jsonObj, writer);
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
writer.close();
return sw.toString();
}
private static void writeJsonToXml(JSONObject jsonObj, XMLStreamWriter writer) throws XMLStreamException {
JSONArray names = jsonObj.names();
for (int i = 0; i < names.length(); i++) {
String name = names.getString(i);
Object value = jsonObj.get(name);
if (value instanceof JSONObject) {
writer.writeStartElement(name);
writeJsonToXml((JSONObject) value, writer);
writer.writeEndElement();
} else if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
writer.writeStartElement("list");
writer.writeAttribute("name", name);
for (int j = 0; j < array.length(); j++) {
writer.writeStartElement("row");
Object obj = array.get(j);
if (obj instanceof JSONObject) {
writeJsonToXml((JSONObject) obj, writer);
}
writer.writeEndElement();
}
writer.writeEndElement();
} else {
writer.writeStartElement(name);
writer.writeCharacters(value.toString());
writer.writeEndElement();
}
}
}
}
```
调用示例:
```java
String jsonStr = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key5\":\"value5\",\"userDataList2\":[{\"key6\":\"value6\",\"userDataList3\":[{\"key9\":\"value9\",\"key8\":\"value8\"},{\"key9\":\"value9\",\"key8\":\"value8\"}],\"key7\":\"value7\"},{\"key6\":\"value6\",\"userDataList4\":[{\"key9\":\"value9\",\"key8\":\"value8\"},{\"key9\":\"value9\",\"key8\":\"value8\"}],\"key7\":\"value7\"}],\"userDataList1\":[{\"key3\":\"value3\",\"key4\":\"value4\"}]}";
System.out.println(JsonToXmlConverter.convert(jsonStr));
```
输出:
```xml
<?xml version="1.0" encoding="GBK"?><stream><key1>value1</key1><key2>value2</key2><list name="userDataList1"><row><key3>value3</key3><key4>value4</key4></row></list><list name="userDataList2"><row><key6>value6</key6><key7>value7</key7><list name="userDataList3"><row><key9>value9</key9><key8>value8</key8></row><row><key9>value9</key9><key8>value8</key8></row></list></row><row><key6>value6</key6><key7>value7</key7><list name="userDataList4"><row><key9>value9</key9><key8>value8</key8></row><row><key9>value9</key9><key8>value8</key8></row></list></row></list><key5>value5</key5></stream>
```
阅读全文