Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

excel data read #172

Open
Alunkal opened this issue Mar 30, 2020 · 2 comments
Open

excel data read #172

Alunkal opened this issue Mar 30, 2020 · 2 comments

Comments

@Alunkal
Copy link

Alunkal commented Mar 30, 2020

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

@Ronin3502
Copy link

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

@pkatre2
Copy link

pkatre2 commented Oct 23, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants