excel 标记语法错误,结构放错位置

e5nqia27  于 2023-03-09  发布在  其他
关注(0)|答案(4)|浏览(139)

如何修复此错误---〉语法错误(在token(s),misplaced construct(s))错误出现在下面指示的行。这段代码是在网上复制的,我正在使用Eclipse,谢谢!

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class EcellTest22 {
     //Blank workbook
    XSSFWorkbook workbook = new XSSFWorkbook();

    //Create a blank sheet
    XSSFSheet sheet = workbook.createSheet("Employee Data");

    //This data needs to be written (Object[])
    Map<String, Object[]> data = new TreeMap<String, Object[]>();
     //
    data.put("1", new Object[]{"ID","NAME", "LASTNAME"}); <--Syntax error on token(s), misplaced construct(s) 

    data.put("2", new Object[]{1, "Amit", "Shukla"});
    data.put("3", new Object[]{2, "Lokesh", "Gupta"});
    data.put("4", new Object[]{3, "John", "Adwards"});
    data.put("5", new Object[]{4, "Brian", "Schultz"});

    //Iterate over data and write to sheet
    Set<String> keyset = data.keySet();

    int rownum = 0;
    for (String key : keyset) 
    {
        //create a row of excelsheet
        Row row = sheet.createRow(rownum++);

        //get object array of prerticuler key
        Object[] objArr = data.get(key);

        int cellnum = 0;

        for (Object obj : objArr) 
        {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String) 
            {
                cell.setCellValue((String) obj);
            }
            else if (obj instanceof Integer) 
            {
                cell.setCellValue((Integer) obj);
            }
        }
    }
    try 
    {
        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(new File("C:\\Documents and Settings\\admin\\Desktop\\imp data\\howtodoinjava_demo.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
    } 
    catch (Exception e)
    {
        e.printStackTrace();
    }

    }

}
8fsztsew

8fsztsew1#

你需要把所有语句放在代码块中声明之后,例如方法块而不是类块。逻辑上,把所有语句放在代码块中可能是有意义的,但是非声明性语句需要放在新块中

private void processFile() {
    data.put("1", new Object[]{"ID","NAME", "LASTNAME"}); // <--Syntax error  
     ...//snip
  } catch (Exception e) {
     e.printStackTrace();
  } 
}
juud5qan

juud5qan2#

问题解决了。我在Eclipse中创建了一个新项目,将POI(jar)添加到库中,语法错误不再显示。

aij0ehis

aij0ehis3#

将所有代码放在一个main方法中:

public static void main(String[] args) {
        //All of your code goes here

}

语句(不包括声明)必须在块内执行。看起来您正在对某些代码进行测试,而这并不意味着是代码中的实际对象,因此您必须将其放在main方法中。

qij5mzcb

qij5mzcb4#

将try和catch块粘贴到任何方法中.上述问题表明您的try块仅位于类中,这就是您获得语法错误消息或深入了解try的原因

public void yourMethod(){    
        try 
         {
           //Write the workbook in file system
           FileOutputStream out = new FileOutputStream(new File("C:\\Documents and Settings\\admin\\Desktop\\imp data\\howtodoinjava_demo.xlsx"));
                    workbook.write(out);
                    out.close();
                    System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
                } 
                catch (Exception e)
                {
                    e.printStackTrace();
            }
        }

相关问题