读取Excel文件并跳过空行但不是空列

时间:2015-07-23 02:25:28

标签: java excel jxl

我希望阅读一个Excel文件并跳过空行。我的代码跳过空单元格,但它也会跳过空列。如何跳过空行但保留空列?我正在使用JXL Java。

page = get(<url goes here>)
page.json()
...

2 个答案:

答案 0 :(得分:1)

试试这个:

for (int i = 0; i < sheet.getRows(); i++) {
    boolean rowEmpty = true;
    String currentRow = "";
    for (int j = 0; j < sheet.getColumns(); j++) {
        Cell cell = sheet.getCell(j, i);
        String con=cell.getContents();
        if(con !=null && con.length()!=0){
            rowEmpty = false;
        }
        currentRow += con + "|";
    }
    if(!rowEmpty) {
        System.out.println(currentRow);
    }
}

你在做的是:

  • 循环遍历行
    • 循环列
      • 如果单元格为空,则打印单元格,否则跳过它(你的continue语句不执行任何操作,因为它是循环中的最后一条指令,并且一旦到达空单元格,break语句就会停止读取该行)

这是做什么的:

  • 循环遍历行
    • 循环列
      • 将单元格附加到行的字符串,如果它是非空的,则将rowEmpty设置为false(因为它至少包含一个非空单元格)
    • 仅在行不为空时打印行

答案 1 :(得分:0)

在 C# 的情况下,这对我有用

public class TestMap extends ApplicationAdapter implements InputProcessor{
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
SpriteBatch sb;
Texture texture;
Sprite sprite;

@Override
public void create () {
    camera = new OrthographicCamera();
    camera.setToOrtho(false,320,320);
    camera.update();
    tiledMap = new TmxMapLoader().load("testmap.tmx");
    tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
    Gdx.input.setInputProcessor(this);
    sb = new SpriteBatch();
    texture = new Texture(Gdx.files.internal("pikachu.png"));
    sprite = new Sprite(texture);
}
@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    tiledMapRenderer.setView(camera);
    tiledMapRenderer.render();
    sb.begin();
    sprite.draw(sb);
    sb.end();
}
@Override
public boolean keyDown(int keycode) {
    return false;
}
@Override
public boolean keyUp(int keycode) {
    if(keycode == Input.Keys.LEFT)
        camera.translate(0,32);
    if(keycode == Input.Keys.RIGHT)
        camera.translate(0,-32);
    if(keycode == Input.Keys.UP)
        camera.translate(32,0);
    if(keycode == Input.Keys.DOWN)
        camera.translate(-32,0);
    if(keycode == Input.Keys.NUM_1)
        tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
    if(keycode == Input.Keys.NUM_2)
        tiledMap.getLayers().get(1).setVisible(!tiledMap.getLayers().get(1).isVisible());
    return false;
}
@Override
public boolean keyTyped(char character) {
    return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}
@Override
public boolean scrolled(int amount) {
    return false;
}
}