i have excel file (.xlsx) with column
Column1 Column2 Column3 Column1
A 50 30 40
B 45 40 20
.......................................
I need to import this excel in MySQL database. I use following code:
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("Data.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator ite = sheet.rowIterator();
while(ite.hasNext()){
Row row = (Row) ite.next();
Iterator<Cell> cite = row.cellIterator();
while(cite.hasNext()){
Cell c = cite.next();
System.out.print(c.toString() +";");
}
System.out.println();
}
fis.close();
}
I need to get in list all row. This code show in console all data from row, but I don't now to add that data in list and list import to MySQL.
Any idea?
I can think of few approaches in order to write this data into the MySQL database but, I will give you the most basic approach to achieve what you desire. You are using Apache POI library in order to log all the data to console.
while (rows.hasNext()) {
String[] rowsVal = new String[Number_of_Columns];
if (row.getCell(0).getStringCellValue().equalsIgnoreCase("Column_Name")) {
row = rows.next();
}
while (cells.hasNext()) {
keyVal[j] = cells.next().getStringCellValue();
j++;
}
}
Above code will store the content of the first row in the keyVal[] array.
Now, you need to include the packages containing the JDBC classes needed for database programming. Following code snippet will be useful to insert records into the table:
String sql = "INSERT INTO Table " + "VALUES ("keyVal[0] + "," + keyVal[1] + "," + keyVal[2] + "," + keyVal[3])";
The above code snippet will help you in inserting the data into the database.
Links for Reference:
https://www.tutorialspoint.com/jdbc/jdbc-insert-records.htm
This is not a direct answer to your question, but may in fact be an even better approach than what you are considering. You may try exporting your Excel worksheet as a CSV file. This means the lines would look like:
Column1,Column2,Column3,Column4
A,50,30,40
With this CSV file in hand, you may then use MySQL's LOAD DATA tool, which allows for rapidly importing CSV or other regular data:
LOAD DATA INFILE 'C:/path/to/Data.xlsx'
INTO TABLE yourTable
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
The motivation for possibly avoiding your current approach is that it is costly to use an Apache library to open an Excel workbook, and then read it. Even after doing this, you still would need to use JDBC to write that data to MySQL.
Related
I have a java class using csv writer which creates csv file.As know everything will be in text format,I want a specific column data to be a hyperlink.For example consider a table with 1 rows and 4 columns
OUTPUT is : when opened in excel .Each entry will be in a new cell
Col1 Col2 Col3 Col4
OUTPUT when opened in Open Office
Col1,Col2,Col3,Col4
The above data is when I open my csv file in excel.This creation is done by my csv writer in java.
What I wanted here is that Col3 should be a hyperlink
Below is my code
public String[] convertEntry(Info ItemInfo) {
String[] columns = new String[NUMBER_OF_COLUMNS];
columns[1] = "Col1;
columns[2] = "Col2";
columns[3] = "Col3";
columns[4] = "Col4";
return columns;
}
Hope am clear.Any help is appreciated
It's ugly but:
this,is,a,=HYPERLINK("http://www.google.com"),test,csv
Will automatically create it. Otherwise you can turn on the configuration for autocompletion in Excel so they always change. But CSV is supposed to be text, I would suggest leaving it as just the URL and having the user know what they are doing to change it.
EDIT:
public String[] convertEntry(Info ItemInfo) {
String[] columns = new String[NUMBER_OF_COLUMNS];
columns[1] = "Col1;
columns[2] = "Col2";
columns[3] = "=HYPERLINK(" + ItemInfo.Col3 + ")";
columns[4] = "Col4";
return columns;
}
Just wrap the value in the =HYPERLINK() function and it will automatically be a link in excel.
In my project I have uploaded the excel sheets and I retrieve by using java, after receiving excel files i just want to create a database table.
These excel sheets contains mark list (each excel sheet contain single subject mark list) like student name, internal,external,result.
if it is one excel sheet then i can easily create a table in db. But i have to create a table from 4 sheets.
i just tried to list the each excel files using for loop and get the data from one by one but its not my solution.
STUDENT NAME CIA ESE TOTAL
AJAY G 46 31 77
AJITH V 41 27 68
AJITH KUMAR 40 26 66
And My Java code is here :
String dirs[] = file.list();
for(String i:dirs) {
// here i can get the file one by one
fis = new FileInputStream(fullpath+"/"+i);
wb = WorkbookFactory.create(fis);
System.out.println(wb.getNumberOfSheets());
sh = wb.getSheet("Sheet1");
int noOfRows = sh.getLastRowNum();
int noOfCols = sh.getRow(7).getLastCellNum();
System.out.println("Rows : "+ noOfRows);
System.out.println("Cols : "+ noOfCols);
}
The above is my excel sheet data and all sheets like the same with different subject (Subject name is the file name so i can take it).
What I expect is to create a single table in db like :
Table name : StudentResult
Stuname | Subject1 | Sub1CIA | Sub1ESE | Subject2 | Sub2CIA | Sub2ESE ...
and so on (get remaining excel sheet column like this.)
Assuming each of the sheets contain the column heading, collect the column headings from all the sheets and make a unique set of them. Then you can use this set to create the database table dynamically executing a DDL using JDBC.
If you want to create the table after the for loop, then declare a Set variable above for- loop and keep adding the column headings from each Sheet to it. Once the control is out of for loop you will have the set with column headings. Here is the pseudo code for this
// Declare a set here
Set<String> dbColumns = new HashSet<String>();
String dirs[] = file.list();
for(String i:dirs) {
// For each sheet
// Get the heading row ( may be col 1 depending on the work sheet)
// Get the cells from this row
// Add the values to the dbColumns set
}
// Now dbColumns contains the list for columns to be created
// Iterate through this set and form the DDL Statement
// Execute the DDL using jdbc
I'm trying to generate excel file with 200k records. But it is taking almost 2 hours to generate the file.
Here is my code of generating excel file.
Workbook workbook=null;
csvFileName = userId+"_Records_"+new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date())+".xls";
path = ReadPropertyFile.getProperties("download.reports.path");
misService.insertXLSRecord(ackNo,"-",null, VspCommonConstants.getIpFromRequest(request),
new Date(), userId,"N",userReportRoleId);
workbook = getWorkbook(path+csvFileName);
Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName(studAppForm.get(0)
.getScheme_Id()+"_"+studAppForm.get(0).getEFP_Scholarship_Name(),'_'));
if(schemeQuestionData.containsKey(currSheetSchemeId))
createXLSHeaders(sheet,schemeQuestionData.get(currSheetSchemeId));
Row row = sheet.createRow(++rowCount);
currAppId=studAppForm.get(j).getApp_Id().toString();
jspTableAppIds.remove(jspTableAppIds.indexOf(new BigInteger(currAppId)));
writeBook(studAppForm.get(j), row);
Here is my createXLSHeaders method to create header
void createXLSHeaders( Sheet sheet, List<SchemeMasterBean> schemeMasterBeanList){
LOGGER.info("Creating XLS SheetHeaders for sheet "+sheet.getSheetName());
// Sheet sheet = workbook.createSheet();
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("APPLICATION ID");
header.createCell(1).setCellValue("APPLICATION STATUS");
header.createCell(2).setCellValue("APPLICATION DATE");
header.createCell(3).setCellValue("SCHEME/SCHOLARSHIP APPLIED");
header.createCell(4).setCellValue("SCHEME ID");
header.createCell(5).setCellValue("STUDENT ID");
header.createCell(6).setCellValue("STUDENT FULL NAME");
.
.
.
62 heading...
int i=73;
if(schemeMasterBeanList!=null)
for(SchemeMasterBean schemeMasterBean :schemeMasterBeanList){
if(!schemeMasterBean.getSmSchemeType().equals("5") &&
!schemeMasterBean.getSmSchemeType().equals("6")){
header.createCell(i).setCellValue(schemeMasterBean.getSmScholarshipName());
i++;
}
}
}
and finally writebook method
private void writeBook(StudentAppFormVsp saf, Row row) throws JSONException {
Cell cell = row.createCell(0);
cell.setCellValue(saf.getApp_Id()!=null?saf.getApp_Id().toString():"");
cell = row.createCell(1);
cell.setCellValue(saf.getApp_Status()!=null?getApplicationStatusMap().get(saf.getApp_Status()):"");
cell = row.createCell(2);
cell.setCellValue(saf.getCrtn_time()!=null?saf.getCrtn_time().toString():"");
cell = row.createCell(3);
cell.setCellValue(saf.getEFP_Scholarship_Name()!=null?saf.getEFP_Scholarship_Name().toString():"");
cell = row.createCell(4);
cell.setCellValue(saf.getScheme_Id()!=null?saf.getScheme_Id().toString():"");
cell = row.createCell(5);
cell.setCellValue(saf.getStud_Id()!=null?saf.getStud_Id().toString():"");
.
.
62 rows
}
How to reduce the excel sheet generation time?
First: play around with memory for the application if possible.
Then: the tip on using a profiler is really worth the effort.
Any DOM, XML, Excel or otherwise often suffer from location references searching from top to the actual position.
Creating a DOM instead of writing sequentially is costly with respect to memory, and can slow things down. Maybe consider this.
You could make two loop: writing to a CSV file, and then creating an XLS(X).
Then you know where the complexity resides.
The following (I rewrote a bit) is slightly suspect: toString + new BigInteger points to a conversion; I hope not from BigInteger to String to BigInteger.
StudentAppFormVsp saf = studAppForm.get(j);
currAppId = saf.getApp_Id().toString();
jspTableAppIds.remove(jspTableAppIds.indexOf(BigInteger.valueOf(currAppId)));
writeBook(saf, row);
I have recently tried to code a small java file which will insert a row into an already existing table in a .odt document. The table itself has 4 rows and 3 column, but I would like to implement a check which will expand that table if the content to be inserted is larger than 4. However, every time I try to get the table's rows, it returns a null pointer. I am not that familiar with UNO api, but as far as i read through the documentation, the class XColumnsAndRowRange should be used in this situation. My code is as follows:
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
try {
Object table = xNamedTables.getByName(tblName);
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
if(flag){
XColumnRowRange xCollumnAndRowRange =(XColumnRowRange)
UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
XTableRows rows = xCollumnAndRowRange.getRows();
System.out.println("Testing if this works");
rows.insertByIndex(4, size-4);
}
I am not sure if I am missing something here or if I should be using a different function.
As Lyrl suggested, this works:
XTableRows rows = xTable.getRows();
Apparently XColumnRowRange is only used for spreadsheets.
Note: With Basic or Python you would not have this problem, because those languages do not need queryInterface. The code would simply be:
table = tables.getByName(tblName)
rows = table.getRows()
I am creating a workbook with a sheet populated data from a data source then creating a second sheet with a pivot table view of that data. Everything works fine, but I can't seem to change the default look of the pivot table. I am trying to get the setting ( Row Labels-->Click one from the list-->Field Settings-->Subtotals-->None and Row Labels-->Click one from the list-->Field Settings-->Layout & Print-->'Show item labels in tabular form' ) checked while creating the pivot table but couldn't find the handle / flag in the POI. Tried finding something under pivotTable.getCTPivotTableDefinition() or pivotTable.getCTPivotTableDefinition().getPivotTableStyleInfo(), but no lock. Please advise if there is a way to set these settings using poi during pivot table creation, not after the fact following the steps mentioned in the parenthesis. Here is my pivot table code :
XSSFSheet sheet = (XSSFSheet)wb.createSheet("Data");
...
...
//filling data sheet, skipping this part as it's not relevant
...
XSSFSheet pivotSheet = (XSSFSheet)wb.createSheet("Pivot Table");
AreaReference source = new AreaReference(sheet.getSheetName()+"!A$1:W$"+String.valueOf(sheet.getLastRowNum()));
CellReference position = new CellReference("A3");
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(source, position);
/* Add filters */
pivotTable.addRowLabel(17);
pivotTable.addRowLabel(20);
pivotTable.addRowLabel(21);
pivotTable.addRowLabel(22);
pivotTable.addRowLabel(13);
pivotTable.addRowLabel(19);
pivotTable.addRowLabel(6);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(18);
pivotTable.addRowLabel(1);
pivotTable.addRowLabel(7);
pivotTable.addRowLabel(9);
Finally figured it out; lack of good documentation forced me to try a zillion things and finally was able to achieve what I wanted; here is the code :
for(CTPivotField ctPivotField:pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldList()){
ctPivotField.setAutoShow(false);
ctPivotField.setOutline(false);
ctPivotField.setSubtotalTop(false);
ctPivotField.setSubtotalCaption("");
}
instead of creating the pivot table every time, I created one template XLS file with all the desired styling and included that in the source, now I am opening that file filling the necessary data in the source tab, and saving the XLS file with the dynamic data with a different name; since the Pivot table tab is marked to refresh when opened, it does the work. Instead of going through the POI API with the limitations on Pivot Tables, creating a template and using it is much easier and flexible if you will generate the same styled pivot table for dynamic data.
#ninjaxelite here how it goes :
List<Object[]> resultSet = //get raw data
XSSFWorkbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream(this.getClass().getResource("/content/XLS_template.xlsx").getPath()));
} catch (FileNotFoundException e1) {
//error
} catch (IOException e1) {
//error
}
Map<String, CellStyle> styles = createStyles(wb); // some local function to get styles
XSSFSheet sheet = (XSSFSheet)wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
int rowNum = 0;
for (Object[] aRow : resultSet) {
rowNum++;
row = sheet.createRow(rowNum);
cell = row.createCell(0);
cell.setCellValue((String)aRow[0]);
cell.setCellStyle(styles.get("cell_normal_centered"));
...
..
.