令牌上的语法错误,错放的构造(s)

时间:2014-02-05 22:18:51

标签: java eclipse excel syntax apache-poi

如何修复此错误--->令牌上的语法错误,错放的构造(s) 错误位于下面指定的行。 注意:此代码已在Web上复制并尝试将其作为学习工具使用 我正在使用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();
    }

    }

}

4 个答案:

答案 0 :(得分:7)

您需要将声明后的所有语句放在代码块中,例如:方法而不是类块。从逻辑上讲,将所有语句放在代码块中可能是有意义的,但非声明性语句需要包含在新块中

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

答案 1 :(得分:2)

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

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

}

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

在任何方法中粘贴try和catch块。以上问题你的尝试只在课堂内阻止,这就是为什么你得到语法错误信息或深入学习尝试

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();
            }
        }