tag:blogger.com,1999:blog-34762437300798932412024-03-13T07:23:08.015-07:00coding 4 javaPankaj Dehariahttp://www.blogger.com/profile/11452457893239880269noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-3476243730079893241.post-83949714524211732942009-05-24T22:17:00.001-07:002009-05-24T22:17:49.985-07:00Read an existing Excel SpreadsheetCreate a new Java project "JavaExcelRead" and save it somewhere, e.g. "c:/temp/lars.xls".<br /><br />Create an excel spreadsheet.<br /><br />Create a package "reader" and the following class "ReadExcel".<br /><br />I assume that the coding is pretty much self-explaining. I tried to add lots of comments to make it easier to understand.For more complex examples have a look at the excellent documentation of the Java Excel API which is also part of the distribution. <br /><br /><br />package reader;<br /><br />import java.io.File;<br />import java.io.IOException;<br /><br />import jxl.Cell;<br />import jxl.CellType;<br />import jxl.Sheet;<br />import jxl.Workbook;<br />import jxl.read.biff.BiffException;<br />import jxl.write.Label;<br />import jxl.write.Number;<br />import jxl.write.WriteException;<br /><br />public class ReadExcel {<br /><br /> private String inputFile;<br /><br /> public void setInputFile(String inputFile) {<br /> this.inputFile = inputFile;<br /> }<br /><br /> public void read() throws IOException, WriteException {<br /> File inputWorkbook = new File(inputFile);<br /> Workbook w;<br /> try {<br /> w = Workbook.getWorkbook(inputWorkbook);<br /> // Get the first sheet<br /> Sheet sheet = w.getSheet(0);<br /> // Loop over first 10 column and lines<br /><br /> for (int j = 0; j < sheet.getColumns(); j++) {<br /> for (int i = 0; i < sheet.getRows(); i++) {<br /> Cell cell = sheet.getCell(j, i);<br /> CellType type = cell.getType();<br /> if (cell.getType() == CellType.LABEL) {<br /> System.out.println("I got a label "<br /> + cell.getContents());<br /> }<br /><br /> if (cell.getType() == CellType.NUMBER) {<br /> System.out.println("I got a number "<br /> + cell.getContents());<br /> }<br /><br /> }<br /> }<br /> } catch (BiffException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /><br /> public static void main(String[] args) throws WriteException, IOException {<br /> ReadExcel test = new ReadExcel();<br /> test.setInputFile("c:/temp/lars.xls");<br /> test.read();<br /> }<br /><br />}Pankaj Dehariahttp://www.blogger.com/profile/11452457893239880269noreply@blogger.com0tag:blogger.com,1999:blog-3476243730079893241.post-63460585577856189512009-05-24T22:14:00.000-07:002009-05-24T22:16:57.116-07:00Create an Excel SpreadsheetCreate a new Java project "JavaExcelWrite".<br /><br />Create a package "writer" and the following class "CreateExcel".<br /><br />I assume that the coding is pretty much self-explaining. I tried to add lots of comments to make it easier to understand.For more complex examples have a look at the excellent documentation of the Java Excel API which is also part of the distribution. <br /><br /><br />package writer;<br /><br />import java.io.File;<br />import java.io.IOException;<br />import java.util.Locale;<br /><br />import jxl.CellView;<br />import jxl.Workbook;<br />import jxl.WorkbookSettings;<br />import jxl.format.UnderlineStyle;<br />import jxl.write.Formula;<br />import jxl.write.Label;<br />import jxl.write.Number;<br />import jxl.write.WritableCellFormat;<br />import jxl.write.WritableFont;<br />import jxl.write.WritableSheet;<br />import jxl.write.WritableWorkbook;<br />import jxl.write.WriteException;<br />import jxl.write.biff.RowsExceededException;<br /><br /><br />public class WriteExcel {<br /><br /> private WritableCellFormat timesBoldUnderline;<br /> private WritableCellFormat times;<br /> private String inputFile;<br /> <br />public void setOutputFile(String inputFile) {<br /> this.inputFile = inputFile;<br /> }<br /><br /> public void write() throws IOException, WriteException {<br /> File file = new File(inputFile);<br /> WorkbookSettings wbSettings = new WorkbookSettings();<br /><br /> wbSettings.setLocale(new Locale("en", "EN"));<br /><br /> WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);<br /> workbook.createSheet("Report", 0);<br /> WritableSheet excelSheet = workbook.getSheet(0);<br /> createLabel(excelSheet);<br /> createContent(excelSheet);<br /><br /> workbook.write();<br /> workbook.close();<br /> }<br /><br /> private void createLabel(WritableSheet sheet)<br /> throws WriteException {<br /> // Lets create a times font<br /> WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);<br /> // Define the cell format<br /> times = new WritableCellFormat(times10pt);<br /> // Lets automatically wrap the cells<br /> times.setWrap(true);<br /><br /> // Create create a bold font with unterlines<br /> WritableFont times10ptBoldUnderline = new WritableFont(<br /> WritableFont.TIMES, 10, WritableFont.BOLD, false,<br /> UnderlineStyle.SINGLE);<br /> timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);<br /> // Lets automatically wrap the cells<br /> timesBoldUnderline.setWrap(true);<br /><br /> CellView cv = new CellView();<br /> cv.setFormat(times);<br /> cv.setFormat(timesBoldUnderline);<br /> cv.setAutosize(true);<br /><br /> // Write a few headers<br /> addCaption(sheet, 0, 0, "Header 1");<br /> addCaption(sheet, 1, 0, "This is another header");<br /> <br /><br /> }<br /><br /> private void createContent(WritableSheet sheet) throws WriteException,<br /> RowsExceededException {<br /> // Write a few number<br /> for (int i = 1; i < 10; i++) {<br /> // First column<br /> addNumber(sheet, 0, i, i + 10);<br /> // Second column<br /> addNumber(sheet, 1, i, i * i);<br /> }<br /> // Lets calculate the sum of it<br /> StringBuffer buf = new StringBuffer();<br /> buf.append("SUM(A2:A10)");<br /> Formula f = new Formula(0, 10, buf.toString());<br /> sheet.addCell(f);<br /> buf = new StringBuffer();<br /> buf.append("SUM(B2:B10)");<br /> f = new Formula(1, 10, buf.toString());<br /> sheet.addCell(f);<br /><br /> // Now a bit of text<br /> for (int i = 12; i < 20; i++) {<br /> // First column<br /> addLabel(sheet, 0, i, "Boring text " + i);<br /> // Second column<br /> addLabel(sheet, 1, i, "Another text");<br /> }<br /> }<br /><br /> private void addCaption(WritableSheet sheet, int column, int row, String s)<br /> throws RowsExceededException, WriteException {<br /> Label label;<br /> label = new Label(column, row, s, timesBoldUnderline);<br /> sheet.addCell(label);<br /> }<br /><br /> private void addNumber(WritableSheet sheet, int column, int row,<br /> Integer integer) throws WriteException, RowsExceededException {<br /> Number number;<br /> number = new Number(column, row, integer, times);<br /> sheet.addCell(number);<br /> }<br /><br /> private void addLabel(WritableSheet sheet, int column, int row, String s)<br /> throws WriteException, RowsExceededException {<br /> Label label;<br /> label = new Label(column, row, s, times);<br /> sheet.addCell(label);<br /> }<br /><br /> public static void main(String[] args) throws WriteException, IOException {<br /> WriteExcel test = new WriteExcel();<br /> test.setOutputFile("c:/temp/lars.xls");<br /> test.write();<br /> System.out<br /> .println("Please check the result file under c:/temp/lars.xls ");<br /> }<br />}Pankaj Dehariahttp://www.blogger.com/profile/11452457893239880269noreply@blogger.com0tag:blogger.com,1999:blog-3476243730079893241.post-61941311393814278222009-04-14T02:34:00.002-07:002009-04-14T02:36:07.133-07:00Count letters in a String/**<br /> *<br /> * @author javadb.com<br /> */<br />public class Main {<br /> <br /> /**<br /> * This method counts the number of letters in a String<br /> */<br /> public void countLetters(String str) {<br /> <br /> if (str == null)<br /> return;<br /> <br /> int counter = 0;<br /> <br /> for (int i = 0; i < str.length(); i++) {<br /><br /> if (Character.isLetter(str.charAt(i)))<br /> counter++;<br /> }<br /> <br /> System.out.println("The input parameter contained " + counter + " letters.");<br /> }<br /><br /> <br /> /**<br /> * @param args the command line arguments<br /> */<br /> public static void main(String[] args) {<br /> new Main().countLetters("abcde123");<br /> }<br />}Pankaj Dehariahttp://www.blogger.com/profile/11452457893239880269noreply@blogger.com0tag:blogger.com,1999:blog-3476243730079893241.post-76751193798290006512009-04-14T02:34:00.001-07:002009-04-14T02:34:55.183-07:00Finding the Most Common Character in a Stringstatic int countChars( String str, char searchChar ) {<br /> // Count the number of times searchChar occurs in<br /> // str and return the result.<br /> int i; // A position in the string, str.<br /> char ch; // A character in the string.<br /> int count; // Number of times searchChar has been found in str.<br /> count = 0;<br /> for ( i = 0; i < str.length(); i++ ) {<br /> ch = str.charAt(i); // Get the i-th character in str.<br /> if ( ch == searchChar )<br /> count++;<br /> }<br /> return count;<br /> }Pankaj Dehariahttp://www.blogger.com/profile/11452457893239880269noreply@blogger.com0tag:blogger.com,1999:blog-3476243730079893241.post-29645785297774501772009-04-14T02:27:00.000-07:002009-04-14T02:34:10.378-07:00Counting all Non-blank Characters in a Stringimport java.util.Scanner;<br /><br />public class Count {<br /><br /> public static void main(String[] args) {<br /> <br /> for (int count = 1; count <= 5; count ++) {<br /> <br /> System.out.println("Please enter some text: ");<br /> Scanner keyboard = new Scanner(System.in);<br /> String text = keyboard.nextLine();<br /> <br /> int HowLong = text.length();<br /> String MinusTrailing = text.trim();<br /> int CMinusTrailing = MinusTrailing.length();<br /><br /> String NB = MinusTrailing;<br /> int countChar = 0;<br /> int limit = NB.length();<br /> <br /> for(int i = 0; i < limit; ++i)<br /> { <br /> if(Character.isWhitespace(NB.charAt(i)))<br /> { <br /> ++count;<br /> }<br /> } <br /> <br /> System.out.println(HowLong);<br /> System.out.println(CMinusTrailing);<br /> System.out.println(countChar );<br /> <br /> <br /> }<br /> <br /> }<br /><br />}Pankaj Dehariahttp://www.blogger.com/profile/11452457893239880269noreply@blogger.com0