Showing posts with label excel. Show all posts
Showing posts with label excel. Show all posts

Friday, August 14, 2015

Excel data to Sqlite3 Database table using Java

Below is the code for inserting data from excel sheet to sqlite3 database using Java code.

1.Open new eclipse Java Project and reference to the latest Java jdbc-sqlite jar file from    below url
   https://bitbucket.org/xerial/sqlite-jdbc/downloads

2. Also reference to the Apache POI - the Java API for Microsoft Documents, which we require for reading through the excel file
    http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.12-20150511.tar.gz

3. Below is the code for the same

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;
import org.sqlite.core.DB;

public class run {
public static void main(String args[]) {
try {

            Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:test.db"); 
PreparedStatement pstm = null; Statement stmt = null; 
con.setAutoCommit(false);

pstm = (PreparedStatement) con.prepareStatement("DROP TABLE IF 
EXISTS table_name"); pstm.execute();

stmt = con.createStatement(); 
String sql = "CREATE TABLE table_name " + "(ID INTEGER PRIMARY KEY 
AUTOINCREMENT," + " NAME         TEXT)"; 
stmt.executeUpdate(sql);
stmt.close();


FileInputStream input = new FileInputStream("C:\\Emp.xls");
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Row row;
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
String col2 = row.getCell(1).getStringCellValue();
String sqlcommand = "INSERT INTO table_name(NAME) Values('" + col2 + "')";
pstm = (PreparedStatement) con.prepareStatement(sqlcommand);
pstm.execute();
System.out.println("Import rows " + i);
}
con.commit();
pstm.close();

con.close();
input.close();
System.out.println("Success import excel to sqlite Database");
} catch (ClassNotFoundException e) {
System.out.println(e);
} catch (SQLException ex) {
System.out.println(ex);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}


Wednesday, August 28, 2013

download an excel file from a ftp in c#.net | Download file from ftp example in .net

hi in this post i will show how to download an excel file from a ftp in c#.net.

Below is the code:

    public void Main()
    {
        // FTP URL
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://www.yourDomainNameorIP/ExcelFile.xlsx");
        ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

        // FTP Login
        ftpRequest.Credentials = new NetworkCredential("<UserId>", "<Pwd>");

        FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

        Stream responseStream = ftpResponse.GetResponseStream();
        long cl = ftpResponse.ContentLength;

        int bufferSize = 2048;
        int readCount;

        byte[] buffer = new byte[bufferSize];

        String FilePath;
        FilePath = Server.MapPath("/Download"); // create a folder called Download in the application root folder to save the file.

        FileStream outputStream = new FileStream(FilePath + "\\" + "filename.xlsx", FileMode.Create);

        readCount = responseStream.Read(buffer, 0, bufferSize);

        while (readCount > 0)
        {
            outputStream.Write(buffer, 0, readCount);
            readCount = responseStream.Read(buffer, 0, bufferSize);
        }

        responseStream.Close();
        outputStream.Close();
        ftpResponse.Close();
    }


After u execute the above code u can see the downloaded file in the download folder created in the root folder of the application.