Copy the columns in Excel 2 into Excel and type in the CSV using Java
< P>
Below is the complete code that writes a new Excel with dummy data, you can use Apache POI, I have the same code for my application Migrated to, which is working fine and added styles too.
HSSFWorkbook Workbook = New HSSFWorkbook (); HSSFSheet sheet = workbook.createSheet ("sample sheet"); Maps & lt; String, object [] = "" & gt; Data = new hashmap & lt; String, object [] = "" & gt; (); Data.put ("1", new object [] {"ampere number", "name", "salary"}); Data.put ("2", New Object [] (1d, "John", 1500000d}); Data.put ("3", New Object [] (2D, "Sam", 800000d}); Set (string key: "string" for "put" ("4", new object [] {3d, "dean", 700000d}); set gtc: keyset = data.keySet (); for input rownum = 0; Keyset) {Line line = Sheet.kreterov (Routine ++); Object [] objArr = data.get (key); for Ent Salonum = 0; (object obj: objArr) {cell cell = row.createCell (Selenam + +); If (obj instanceof date) cell.setCellValue ((date) obj); and if (obj instanceof boolean) cell.setCellValue obj (boolean); and if (obj instance string) cell.setCellVa Lue (string) obj; and if (obje example double) cell. Sitelvel (double) obj);}} {FileOutputStream Out = New FileOutputStream (new file ("C: \\ new.xls")) Try; workbook.write (out); out.close (); System.out.println ("Writing Excel successfully ..");} Hold (FileNotFoundException e) {e.printStackTrace ();} Hold (IOException e) {E.printStackTrace ();} & lt; / string & gt; & lt; / string, & gt; & Lt; / String, & gt;
Here comes my scala code.
> XlsCommand takes the form as a parameter and returns the string. * This method is to export the data in Excel * Used apache.poi to set the value for Excel. * Output path is hard for now. * / Def xslExport (CMD: List [XlsCommand]): [string] = {try (Dbkwith session (contained session = & gt; {val depts = ReadOnlyDepartmentRepository.findAllQuery (). List Val workbook = new HSSFWorkbook () Val sheet = Workbook.createSheet ( "intervention details") val cellStyle = workbook.createCellStyle () cellStyle.setFillForegroundColor (HSSFColor.GOLD.index) val data: Map [string, Array [_]] = new Hasmap [string, Array [ _]] () Var id = 1 cmd.map {cmd = & gt; id = id = 1 val deptName = depts.find (y = & gt; some (y.id) == cmd.department) .map ( _. Data) data input ("1", array ("interference", "department", "employee number", "name", "id Mr. Number "," expiration date ")) data. Input (id.toString, Array (cmd. EmployeeInterventionId.getOrElse (" NotAvailable "), deptName, cmd.employeeNumber.getOrElse (" NotAvailable "), cmd.firstName, cmd. idNumber.getOrElse ( "NotAvailable"), cmd.expiryDate.getOrElse ( "NotAvailable"). toString))} enmity Sortdedeta = Trymark (data. Tiiik: _ *) var line New: Int = 0 Sorteddetakmap {case (key , Value) = & gt; Val line: line = sheet Cutter (Row Number) Row Number = Row + 1 Val objArr = Sort Data DataTat (Key). Cell Cell Num .: Int = 0 objArr.map {obj = & gt; Val cell = row.createCell (cellNum) cellNum = cellNum + 1 if (obj.isInstanceOf [string]) {cell.setCellStyle (cellStyle) cell.setCellValue (obj.toString)} else if (obj.isInstanceOf [Long]) { Cell.setCellValue (obj.asInstanceOf [Long])} if (obj.isInstanceOf [date]) {cell .setCellValue (obj.asInstanceOf [date]) and that (obj.isInstanceOf [Int]) {cell.setCellValue (obj .asInstanceOf [int])}}} Val Path = ConfigFactory.load.getString ("upload.excelLocation") Val = New FileOutputStream (new file (path + File.separator + "interventionDetails.xls")) workbook.write ( Out), out.close () "success"})}} for reference.
Hope I got help.
No comments:
Post a Comment