rampatra/jbot

excel data read

Alunkal opened this issue · 2 comments

could anyone help me to read data from excel in this framework for slackbot

Hi, maybe you can try to add this piece of code in SlackBot.java in the jbot-example.
This piece of code can update a .xls file from the bot to slack.
The .xls file is named "test.xls", and is located in the "E:/test.xls" at the host which is running SlackBot.java
Besides, you need to add a jar named "jxl.jar" and some dependencies as follow:

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import me.ramswaroop.jbot.core.common.Controller;
import me.ramswaroop.jbot.core.common.EventType;
import me.ramswaroop.jbot.core.common.JBot;
import me.ramswaroop.jbot.core.slack.Bot;
import me.ramswaroop.jbot.core.slack.models.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.web.socket.WebSocketSession;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
    public void postExcelToSlack(WebSocketSession session, Event event) throws IOException {
        File file = new File("E:/test.xls");
        okhttp3.Response response = new Meteoroid.Builder()
                .token(slackToken)
                .channels(event.getChannelId())
                .uploadFile(file)
                .build()
                .post();
        //startConversation(event, "confirmPosition");   // start conversation
        reply(session, event, "excel file received!");
        String result;
        if(event.getText().contains(",")){
            String x="";
            String y="";
            int i;
            int flagg=0;
            for(i=0; i<event.getText().length(); i++)
            {
                if(event.getText().charAt(i)>='0' && event.getText().charAt(i)<='9' && flagg==0)
                {
                    x+=event.getText().charAt(i);
                }
                else if(event.getText().charAt(i)==',')
                    flagg++;
                else if(event.getText().charAt(i)>='0' && event.getText().charAt(i)<='9' && flagg==1)
                {
                    y+=event.getText().charAt(i);
                }
            }
            reply(session, event, x + "," + y);
            try
            {
                Workbook book=Workbook.getWorkbook(new File("E:/test.xls"));
                Sheet sheet=book.getSheet(0);
                Cell cell1=sheet.getCell(Integer.parseInt(y),Integer.parseInt(x));
                result=cell1.getContents();
                book.close();
            }catch(Exception e)
            {
                System.out.println(e);
                result="none";
            }
            reply(session, event, "the element in the " + x + "line " + y + "colum is: "+result);
        }
        response.close();
    }

There are two ways to run it,
First, if you simply type in "excel", the bot will just transmit the .xls file
Second, if you type in "excel x,y", the bot will transmit the .xls file and the element located at (x,y) in the file.
the following two figures are the corresponding results of the above two ways:
test2
test1

can you please assign it to me , would like to contribute