| Author |
Message |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26962 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 05:11 pm: |
    |
Myselfme:if(!cellContent.equals("")) sb.append("<"+colname+">"+ cellContent + "<"+colname+">\n");
Man ur awesome code is working like GEM. Cheat code kakunda other code time unnappudu update cheyu n keep in touch ya..till this project gets over. Once again Thank You Myselfme. |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 127 Registered: 04-2011 Posted From: 166.147.104.154
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 04:58 pm: |
    |
colName with capital N, post automatically converting to lower case |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 126 Registered: 04-2011 Posted From: 166.147.104.157
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 04:54 pm: |
    |
See lines in red Sorry I cannot use my office computer, posting from my phone, there are typos if(cellContent.equals("")) sb.append("<"+colname+">"+cellContent+"</"+colname+">\n"); |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 125 Registered: 04-2011 Posted From: 166.147.104.159
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 04:47 pm: |
    |
Posted on Monday, November 25, 2013 - 05:00 pm: Myselfme: My final code: public static void main(String [] args) throws Exception { String xml=""; StringBuilder sb = new StringBuilder(); try { FileInputStream file = new FileInputStream(new File("C:\temp\Simple.xlsx")); //Get the workbook instance for XLS file XSSFWorkbook wb = new XSSFWorkbook(file); Sheet s = wb.getSheetAt(0);// workbook.getSheet(sheetName); sb.append("<?xml>\n"); sb.append("<data>\n"); int count = 0; int rowCount = 0; // First row contains column names Row row = s.getRow(0); int noofrows = s.getPhysicalNumberOfRows(); int numColumns = row.getPhysicalNumberOfCells(); if (row == null) throw new Exception("Cannot get row 0 from sheet " + s); String colNames[] = new String[numColumns]; Cell[] cells = new Cell[numColumns]; // Get column names from first row for (int i = 0; i < numColumns; ++i) { cells[i] = row.getCell(i); String cname = cells[i].getStringCellValue(); if (cells[i].getCellType() != Cell.CELL_TYPE_BLANK) { String colName = cname.trim(); // Make sure column name contains only allowed characters. colNames[i] = colName; /* validate columsn**/ } } // Loop over rows in this sheet Row tempRow; for (int i = 1; i < noofrows; i++) { tempRow = s.getRow(i); Cell cell = tempRow.getCell(0); //if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) //continue; int excelRow = i + 1; // bw.write(" <row>\n"); /* write data to xml format required here */ // Loop over columns in this row for (int j = 0; j < numColumns; ++j) { // In case this row has more // columns than the fist row. // Then ignore this extra column. if (j >= numColumns) continue; cell = tempRow.getCell(j); String colName = colNames[j]; String cellContent = ""; if (cell != null) { int celltype = cell.getCellType(); if (j == 0 && celltype == Cell.CELL_TYPE_BLANK) { rowCount++; } if (celltype == Cell.CELL_TYPE_STRING) { if (cell.getStringCellValue() != null) //cellContent = xmlEncode(cell.getStringCellValue()); cellContent = cell.getStringCellValue(); else cellContent = ""; } else if (celltype == Cell.CELL_TYPE_NUMERIC) { cellContent = Double.toString(cell .getNumericCellValue()); } else { cellContent = ""; } } else { if (j == 0) rowCount++; } // bw.write(" <">" + cellContent + "</" + // colName + ">\n"); If(cellContent.equals("") sb.append("<"+colname+">"+ cellContent + "</"+colname+">"); } // bw.write(" </row>\n"); //TEMP sb.append("</xxxxx>\n"); } sb.append("</data>\n"); }catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(sb); } }} |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26961 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 03:20 pm: |
    |
Myselfme:sb.append("<"+colname+">"+cell.getStringCellValue()+"</"+col name+">\n"); Try above line
Master Post Number: 26955 lo naa final code chudandi and colname is something defined down below but this cheat code top lo use cheyamantunnaru..so colname top lo define ekkada ela cheyali?thx |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 124 Registered: 04-2011 Posted From: 166.147.104.172
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 10:10 am: |
    |
colName? Or colname? Case sensitive |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 123 Registered: 04-2011 Posted From: 166.147.104.172
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 10:07 am: |
    |
sb.append("<"+colname+">"+cell.getStringCellValue()+"</"+col name+">\n"); Try above line |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26960 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 09:54 am: |
    |
Myselfme: if(!cellContent.equals("")) sb.append("<"+colname+">"+ cellContent + "<"+colname+">\n"); Prasthuthaniki cheating....remaining I will try tomorrow
Code ni below ga change chesa but ????? place lo emi ravali lo artham kadam ledu if(!cell.getStringCellValue().equals("")) sb.append("<???>"+ cell.getStringCellValue() + "</???>\n"); |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 122 Registered: 04-2011 Posted From: 98.206.237.162
Rating: N/A Votes: 0 (Vote!) | | Posted on Tuesday, November 26, 2013 - 12:34 am: |
    |
//if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) //continue; ******* if(!cellContent.equals("")) sb.append("<"+colname+">"+ cellContent + "<"+colname+">\n"); ******* Prasthuthaniki cheating....remaining I will try tomorrow |
   
Botsa_fan
Junior Artist Username: Botsa_fan
Post Number: 187 Registered: 07-2013 Posted From: 24.165.127.206
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 09:22 pm: |
    |
http://viralpatel.net/blogs/java-read-write-excel-file-apach e-poi/ |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26956 Registered: 10-2007 Posted From: 75.181.140.190
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 08:19 pm: |
    |
Myselfme:I looked at the fonts, not the merged cell, I will work and send you new code, But how font forma should be written depends on XML parser?
lets forget about font formation right now...ur awesome bro, thanks a lot |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 121 Registered: 04-2011 Posted From: 166.147.104.145
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 08:13 pm: |
    |
I looked at the fonts, not the merged cell, I will work and send you new code, But how font forma should be written depends on XML parser? |
   
Thakita_thakita
Comedian Username: Thakita_thakita
Post Number: 1239 Registered: 07-2011 Posted From: 148.106.4.6
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 05:03 pm: |
    |
this is going to be very complicated one. As you have merged cell and some non merged cells in there. Code provided in this thread won't work. You need to use merged content feature in POI |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26955 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 05:00 pm: |
    |
Myselfme:
My final code: public static void main(String [] args) throws Exception { String xml=""; StringBuilder sb = new StringBuilder(); try { FileInputStream file = new FileInputStream(new File("C:\temp\Simple.xlsx")); //Get the workbook instance for XLS file XSSFWorkbook wb = new XSSFWorkbook(file); Sheet s = wb.getSheetAt(0);// workbook.getSheet(sheetName); sb.append("<?xml>\n"); sb.append("<data>\n"); int count = 0; int rowCount = 0; // First row contains column names Row row = s.getRow(0); int noofrows = s.getPhysicalNumberOfRows(); int numColumns = row.getPhysicalNumberOfCells(); if (row == null) throw new Exception("Cannot get row 0 from sheet " + s); String colNames[] = new String[numColumns]; Cell[] cells = new Cell[numColumns]; // Get column names from first row for (int i = 0; i < numColumns; ++i) { cells[i] = row.getCell(i); String cname = cells[i].getStringCellValue(); if (cells[i].getCellType() != Cell.CELL_TYPE_BLANK) { String colName = cname.trim(); // Make sure column name contains only allowed characters. colNames[i] = colName; /* validate columsn**/ } } // Loop over rows in this sheet Row tempRow; for (int i = 1; i < noofrows; i++) { tempRow = s.getRow(i); Cell cell = tempRow.getCell(0); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) continue; int excelRow = i + 1; // bw.write(" <row>\n"); /* write data to xml format required here */ // Loop over columns in this row for (int j = 0; j < numColumns; ++j) { // In case this row has more // columns than the fist row. // Then ignore this extra column. if (j >= numColumns) continue; cell = tempRow.getCell(j); String colName = colNames[j]; String cellContent = ""; if (cell != null) { int celltype = cell.getCellType(); if (j == 0 && celltype == Cell.CELL_TYPE_BLANK) { rowCount++; } if (celltype == Cell.CELL_TYPE_STRING) { if (cell.getStringCellValue() != null) //cellContent = xmlEncode(cell.getStringCellValue()); cellContent = cell.getStringCellValue(); else cellContent = ""; } else if (celltype == Cell.CELL_TYPE_NUMERIC) { cellContent = Double.toString(cell .getNumericCellValue()); } else { cellContent = ""; } } else { if (j == 0) rowCount++; } // bw.write(" <">" + cellContent + "</" + // colName + ">\n"); sb.append("<"+colname>" + cellContent + "</">\n"); } // bw.write(" </row>\n"); //TEMP sb.append("</xxxxx>\n"); } sb.append("</data>\n"); }catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(sb); } } Above code is generating below xml <?xml> <data> <integrationcode>D217</integrationcode> <description>Desc1</description> <highlighttext>Highlight1</highlighttext> <sequence>1.0</sequence> <promotion>Promotion1</promotion> <integrationcode>V217</integrationcode> <description>Desc2</description> <highlighttext>Highlight1</highlighttext> <sequence>1.0</sequence> <promotion>Promotion2</promotion> <integrationcode>S217</integrationcode> <description>Desc3</description> <highlighttext>Highlight1</highlighttext> <sequence>1.0</sequence> <promotion>Promotion3</promotion> </data> HighlightText is not repeating, even though it has multiple data lines |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 120 Registered: 04-2011 Posted From: 166.147.104.143
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 02:25 pm: |
    |
bw.append continuation went to next line, just bring it to same line |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 119 Registered: 04-2011 Posted From: 166.147.104.147
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 02:21 pm: |
    |
int rowCount = 0; String sheetName = "Sheet1"; // you can automatically get this xmlEncode is for replacing any extra characters like &, <,> which will screw up XML parsers, you can override it later just comment it out |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26953 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 12:44 pm: |
    |
> rowCount var ledu > xmlEncode function ledu > bw.append formation looks odd to me and formatting error too. |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26952 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Monday, November 25, 2013 - 12:40 pm: |
    |
public void convert(String input, String output) throws Exception { Workbook wbs; InputStream inp = new FileInputStream(input); wbs = WorkbookFactory.create(inp); /* * if(isXlsx(input)) wbs = new XSSFWorkbook(inp); else { POIFSFileSystem * fs = new POIFSFileSystem(inp); wbs = new HSSFWorkbook(fs); } */ StringBuffer warnings = new StringBuffer(); writeXml(wbs, output); inp.close(); } /** * Parse a workbook and write out contents into an xml file. */ private void writeXml(Workbook workbook, String output) throws Exception { if (workbook == null) throw new Exception("Null workbook"); if (output == null) throw new Exception("Null xml filename"); if (output.length() == 0) throw new Exception("Zero length xml filename"); FileOutputStream out = new FileOutputStream(output); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, "UTF8")); try { Sheet s = workbook.getSheetAt(0);// workbook.getSheet(sheetName); if (s == null) throw new Exception("Cannot get sheet " + sheetName + " from this workbook"); bw.write("<?xml>\n"); bw.write("<data>\n"); int count = 0; // First row contains column names Row row = s.getRow(0); int noofrows = s.getPhysicalNumberOfRows(); int numColumns = row.getPhysicalNumberOfCells(); if (row == null) throw new Exception("Cannot get row 0 from sheet " + sheetName); String colNames[] = new String[numColumns]; Cell[] cells = new Cell[numColumns]; // Get column names from first row for (int i = 0; i < numColumns; ++i) { cells[i] = row.getCell(i); if (cells[i] == null) { Log.warn("Column name " + i + " is null"); continue; } String cname = cells[i].getStringCellValue(); if (cells[i].getCellType() != Cell.CELL_TYPE_BLANK) { String colName = cname.trim(); // Make sure column name contains only allowed characters. colNames[i] = colName; /* validate columsn**/ } } // Loop over rows in this sheet Row tempRow; for (int i = 1; i < noofrows; i++) { tempRow = s.getRow(i); Cell cell = tempRow.getCell(0); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) continue; int excelRow = i + 1; // bw.write(" <row>\n"); /* write data to xml format required here */ // Loop over columns in this row for (int j = 0; j < numColumns; ++j) { // In case this row has more // columns than the fist row. // Then ignore this extra column. if (j >= numColumns) continue; cell = tempRow.getCell(j); String colName = colNames[j]; String cellContent = ""; if (cell != null) { int celltype = cell.getCellType(); if (j == 0 && celltype == Cell.CELL_TYPE_BLANK) { rowCount++; } if (celltype == Cell.CELL_TYPE_STRING) { if (cell.getStringCellValue() != null) cellContent = xmlEncode(cell .getStringCellValue()); else cellContent = ""; } else if (celltype == Cell.CELL_TYPE_NUMERIC) { cellContent = Double.toString(cell .getNumericCellValue()); } else { cellContent = ""; } } else { if (j == 0) rowCount++; } // bw.write(" <">" + cellContent + "</" + // colName + ">\n"); bw.append("<">" + cellContent + "</" + colName + ">\n"); } // bw.write(" </row>\n"); bw.write("</xxxxx>\n"); } bw.write("</data>\n"); bw.flush(); } catch (Exception e) { throw new Exception("Cannot convert excel data to xml: " + e.getMessage()); } finally { bw.close(); } -------------------- Myselfme, Could you take a look at those RED lines?? Thx |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26951 Registered: 10-2007 Posted From: 75.181.140.190
Rating: N/A Votes: 0 (Vote!) | | Posted on Sunday, November 24, 2013 - 09:47 pm: |
    |
Myselfme:If you have teamviewer installed on your laptop I will show you , give me your email
Thx annai, but I want try myself first ..tomorrow jdev or eclipse lo try chesta..doubts vaste post cjesta..appatiki problem unte desktop chare chesta. Once again Thx bro. |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 118 Registered: 04-2011 Posted From: 98.206.237.162
Rating: N/A Votes: 0 (Vote!) | | Posted on Sunday, November 24, 2013 - 08:56 pm: |
    |
My functions will work , if you spend an hour on the code below, problem is I cannot provide some missing statements, copy these to eclipse, call these functions from from main function , add apache poi jars to path, remove any statements creating problem, You should know statement by statement from beginning itself, otherwise , if you want change something, it's difficult , If you have teamviewer installed on your laptop I will show you , give me your email |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26950 Registered: 10-2007 Posted From: 75.181.140.190
Rating: N/A Votes: 0 (Vote!) | | Posted on Sunday, November 24, 2013 - 08:07 pm: |
    |
bump tp keep active.. |
   
One
Hero Username: One
Post Number: 16388 Registered: 09-2008 Posted From: 192.210.137.161
Rating: N/A Votes: 0 (Vote!) | | Posted on Saturday, November 23, 2013 - 01:08 pm: |
    |
J$F, easy thing to do .. first try cheyi .. raka pothey post cheyi ekkada struck ayyavo .. motham evadoo chesthey neku ela vasthundi |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26949 Registered: 10-2007 Posted From: 75.181.140.190
Rating: N/A Votes: 0 (Vote!) | | Posted on Saturday, November 23, 2013 - 12:15 pm: |
    |
Dma:you want an application or one time deal?
nope on demand annattu..3rd party will call through webservice we have to pass all the excel info annatu |
   
Dma
Hero Username: Dma
Post Number: 10929 Registered: 11-2009 Posted From: 72.201.132.90
Rating: N/A Votes: 0 (Vote!) | | Posted on Saturday, November 23, 2013 - 03:48 am: |
    |
Just4fun:itta kuda seyachha ..inkoncham li8 eyanadi master
you want an application or one time deal? if it is one time, yes. open in excel and save as xml, manually. Vote for Decoits... Bring back Pizzaa Rule... Keep Decoits Away... Keep Congress Away... sthothram sthothram sthothram Jai Andhra!! Jai Jai Andhra!!! |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26947 Registered: 10-2007 Posted From: 75.181.140.190
Rating: N/A Votes: 0 (Vote!) | | Posted on Saturday, November 23, 2013 - 02:39 am: |
    |
Abhysg:Open excel save as Xml Khel khatam
itta kuda seyachha ..inkoncham li8 eyanadi master |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26946 Registered: 10-2007 Posted From: 75.181.140.190
Rating: N/A Votes: 0 (Vote!) | | Posted on Saturday, November 23, 2013 - 02:38 am: |
    |
Cyberabadsinnodu:nenu chestunna mama
Awesome mama.. Emc2:String builder use chesi XML create cheyyi ess,first data extract cheyyi, Mari spoon feeding ante ela
Master Java edo bommal chusi nerchukunnam..not an expert. Will try String builder |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26945 Registered: 10-2007 Posted From: 75.181.140.190
Rating: N/A Votes: 0 (Vote!) | | Posted on Saturday, November 23, 2013 - 02:36 am: |
    |
Cocanada:You can do programming in excel using VBA
Cokesh, converting to xml in JAVA will make my task easy, as I have to expose those values as WEB Service |
   
Abhysg
Hero Username: Abhysg
Post Number: 12926 Registered: 08-2008 Posted From: 198.228.200.20
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 07:41 pm: |
    |
Open excel save as Xml Khel khatam no siggy |
   
Emc2
Megastar Username: Emc2
Post Number: 20534 Registered: 03-2008 Posted From: 71.246.229.243
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 04:54 pm: |
    |
Just4fun:
String builder use chesi XML create cheyyi ess,first data extract cheyyi, Mari spoon feeding ante ela |
   
Emc2
Megastar Username: Emc2
Post Number: 20533 Registered: 03-2008 Posted From: 71.246.229.243
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 04:53 pm: |
    |
Cocanada:
Oopukuntu half knowledge tho vachesav ,out amma |
   
Cyberabadsinnodu
Side Hero Username: Cyberabadsinnodu
Post Number: 9906 Registered: 02-2010 Posted From: 63.243.17.163
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 04:50 pm: |
    |
Just4fun:@Other JAVA experts, excel sheet ichha, xml format ichha..bore kodite kasta POC chesi code pass cheyandi babulu
nenu chestunna mama...let me see how it goes... Chudappa Dber appa...nenu NRI ne kani...allu DB sudaru nenu chusta anthe teda..aina last post manadhi aithe daniki vache kick ee verappa.. |
   
Cocanada
Legend Username: Cocanada
Post Number: 43958 Registered: 01-2008 Posted From: 168.244.164.254
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 04:23 pm: |
    |
excel is binary file. can we read it as byte stream???? |
   
Cocanada
Legend Username: Cocanada
Post Number: 43957 Registered: 01-2008 Posted From: 168.244.164.254
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 04:10 pm: |
    |
you need a VBA expert. Not Java expert You can do programming in excel using VBA |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26944 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 04:06 pm: |
    |
@Other JAVA experts, excel sheet ichha, xml format ichha..bore kodite kasta POC chesi code pass cheyandi babulu |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26943 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 04:06 pm: |
    |
@Emc2, nuvvu cheppina links lo general ga ela cheyachho undi..but exact naa req format loki convert cheyalantE java skills kavali..manaki li8 ga telusu @Myselfme, nee code edo exact ga naaku kavalsinattu unndi will do some POC , doubts unte aduguta |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 117 Registered: 04-2011 Posted From: 166.147.104.157
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:50 pm: |
    |
I have few conditions for excel, remove them |
   
Myselfme
Junior Artist Username: Myselfme
Post Number: 116 Registered: 04-2011 Posted From: 166.147.104.157
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:48 pm: |
    |
you can use below code /* input : excel file * output : xml file */ public void convert(String input, String output) throws Exception { Workbook wbs; InputStream inp = new FileInputStream(input); wbs = WorkbookFactory.create(inp); /* * if(isXlsx(input)) wbs = new XSSFWorkbook(inp); else { POIFSFileSystem * fs = new POIFSFileSystem(inp); wbs = new HSSFWorkbook(fs); } */ StringBuffer warnings = new StringBuffer(); writeXml(wbs, output); inp.close(); } /** * Parse a workbook and write out contents into an xml file. */ private void writeXml(Workbook workbook, String output) throws Exception { if (workbook == null) throw new Exception("Null workbook"); if (output == null) throw new Exception("Null xml filename"); if (output.length() == 0) throw new Exception("Zero length xml filename"); FileOutputStream out = new FileOutputStream(output); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, "UTF8")); try { Sheet s = workbook.getSheetAt(0);// workbook.getSheet(sheetName); if (s == null) throw new Exception("Cannot get sheet " + sheetName + " from this workbook"); bw.write("<?xml>\n"); bw.write("<data>\n"); int count = 0; // First row contains column names Row row = s.getRow(0); int noofrows = s.getPhysicalNumberOfRows(); int numColumns = row.getPhysicalNumberOfCells(); if (row == null) throw new Exception("Cannot get row 0 from sheet " + sheetName); String colNames[] = new String[numColumns]; Cell[] cells = new Cell[numColumns]; // Get column names from first row for (int i = 0; i < numColumns; ++i) { cells[i] = row.getCell(i); if (cells[i] == null) { Log.warn("Column name " + i + " is null"); continue; } String cname = cells[i].getStringCellValue(); if (cells[i].getCellType() != Cell.CELL_TYPE_BLANK) { String colName = cname.trim(); // Make sure column name contains only allowed characters. colNames[i] = colName; /* validate columsn**/ } } // Loop over rows in this sheet Row tempRow; for (int i = 1; i < noofrows; i++) { tempRow = s.getRow(i); Cell cell = tempRow.getCell(0); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) continue; int excelRow = i + 1; // bw.write(" <row>\n"); /* write data to xml format required here */ // Loop over columns in this row for (int j = 0; j < numColumns; ++j) { // In case this row has more // columns than the fist row. // Then ignore this extra column. if (j >= numColumns) continue; cell = tempRow.getCell(j); String colName = colNames[j]; String cellContent = ""; if (cell != null) { int celltype = cell.getCellType(); if (j == 0 && celltype == Cell.CELL_TYPE_BLANK) { rowCount++; } if (celltype == Cell.CELL_TYPE_STRING) { if (cell.getStringCellValue() != null) cellContent = xmlEncode(cell .getStringCellValue()); else cellContent = ""; } else if (celltype == Cell.CELL_TYPE_NUMERIC) { cellContent = Double.toString(cell .getNumericCellValue()); } else { cellContent = ""; } } else { if (j == 0) rowCount++; } // bw.write(" <">" + cellContent + "</" + // colName + ">\n"); bw.append("<">" + cellContent + "</" + colName + ">\n"); } // bw.write(" </row>\n"); bw.write("</xxxxx>\n"); } bw.write("</data>\n"); bw.flush(); } catch (Exception e) { throw new Exception("Cannot convert excel data to xml: " + e.getMessage()); } finally { bw.close(); } |
   
Emc2
Megastar Username: Emc2
Post Number: 20532 Registered: 03-2008 Posted From: 128.229.4.22
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:37 pm: |
    |
other option is xl ni xml loki impor chesi chudu,you will get an idea how it looks like XL lo ee option undi chuu |
   
Emc2
Megastar Username: Emc2
Post Number: 20531 Registered: 03-2008 Posted From: 128.229.4.22
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:35 pm: |
    |
Just4fun:
HSSFWorkbook use cheyyi, you will see the properties, http://viralpatel.net/blogs/java-read-write-excel-file-apach e-poi/ |
   
Rudraksha
Side Hero Username: Rudraksha
Post Number: 2039 Registered: 07-2012 Posted From: 70.181.57.58
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:34 pm: |
    |
http://www.onjava.com/pub/a/onjava/2003/04/16/poi_excel.html |
   
Rudraksha
Side Hero Username: Rudraksha
Post Number: 2038 Registered: 07-2012 Posted From: 70.181.57.58
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:27 pm: |
    |
Just4fun:any example or blog post ??
http://poi.apache.org/spreadsheet/how-to.html#sxssf |
   
Rudraksha
Side Hero Username: Rudraksha
Post Number: 2037 Registered: 07-2012 Posted From: 70.181.57.58
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:26 pm: |
    |
Emc2:
ante ippudu xml lo kooda colored tags create cheyala |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26942 Registered: 10-2007 Posted From: 204.235.114.65
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:25 pm: |
    |
Emc2:simple POI use chesi read the xl cell properties and populate those as xml tag attribute,
any example or blog post ?? @Rudra, not in csv..its EXCEL |
   
Rudraksha
Side Hero Username: Rudraksha
Post Number: 2036 Registered: 07-2012 Posted From: 70.181.57.58
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:25 pm: |
    |
Emc2:CSV lo neeku bold italic properties ekkadanunchi vasthayi, think think
 |
   
Emc2
Megastar Username: Emc2
Post Number: 20530 Registered: 03-2008 Posted From: 128.229.4.22
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:24 pm: |
    |
Rudraksha:is it comma separated data
CSV lo neeku bold italic properties ekkadanunchi vasthayi, think think |
   
Rudraksha
Side Hero Username: Rudraksha
Post Number: 2035 Registered: 07-2012 Posted From: 70.181.57.58
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:22 pm: |
    |
is it comma separated data (CSV)?? |
   
Emc2
Megastar Username: Emc2
Post Number: 20528 Registered: 03-2008 Posted From: 128.229.4.22
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:08 pm: |
    |
simple POI use chesi read the xl cell properties and populate those as xml tag attribute, you can use those later use for rebuilding the xl, |
   
Just4fun
Megastar Username: Just4fun
Post Number: 26941 Registered: 10-2007 Posted From: 204.235.114.64
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, November 22, 2013 - 03:04 pm: |
    |
I have a ExcelSheet (Simple.xlsx) looks like below
| IntegrationCode | Description | HighlightText | Squence | Promotion | | A217 | Desc1 | Highlight1 | 1 | Promotion1 | | | Highlight2 | 2 | | | | Highlight3 | 3 | | | | Highlight4 | 4 | | | D217 | Desc2 | Highlight1 | 1 | Promotion2 | | | Highlight2 | 2 | | | | Highlight3 | 3 | | | S217 | Desc3 | Highlight1 | 1 | Promotion3 | | | Highlight2 | 2 | | | Need to convert above excel to xml* like below using Java, Apache POI, and Apache Tika <getoffercontent> <offer> <integrationcode>A217</integrationcode> <description>Desc1</description> <highlights> <highlighttext>Highlight1</highlighttext> <sequence>1</sequence> <highlighttext>Highlight2</highlighttext> <sequence>2</sequence> <highlighttext>Highlight3</highlighttext> <sequence>3</sequence> <highlighttext>Highlight4</highlighttext> <sequence>4</sequence> </highlights> <promotion>Promotion1</promotion> </offer> <offer> <integrationcode>D217</integrationcode> <description>Desc2</description> <highlights> <highlighttext>Highlight1</highlighttext> <sequence>1</sequence> <highlighttext>Highlight2</highlighttext> <sequence>2</sequence> <highlighttext>Highlight3</highlighttext> <sequence>3</sequence> </highlights> <promotion>Promotion2</promotion> </offer> <offer> <integrationcode>S217</integrationcode> <description>Desc3</description> <highlights> <highlighttext>Highlight1</highlighttext> <sequence>1</sequence> <highlighttext>Highlight2</highlighttext> <sequence>2</sequence> </highlights> <promotion>Promotion3</promotion> </offer> </getoffercontent> ----------------------------- * Have to read bold , italic, color as it is ** excel sheet can be download at https://drive.google.com/file/d/0B6weOGC6OtYaa19ZY3Q3QjRoT2M /edit?usp=sharing JAVA experts randi babu randi ..mee tadaka chupinchandi |