java_CreateExcel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileOutputStream; | |
import org.apache.poi.xssf.usermodel.XSSFCell; | |
import org.apache.poi.xssf.usermodel.XSSFRow; | |
import org.apache.poi.xssf.usermodel.XSSFSheet; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
public class excel_Functions1 { | |
public static void main(String[] args) | |
{ | |
String SheetName = "MySheet"; | |
try | |
{ | |
XSSFWorkbook myWorkBook = new XSSFWorkbook(); | |
XSSFSheet mySheet = myWorkBook.createSheet(SheetName); | |
// Create XSSFRow and XSSFCell | |
XSSFRow myRow = null; | |
XSSFCell myCell = null; | |
String excelData[] = new String[3]; | |
excelData[0] = "First Name"; | |
excelData[1] = "Last Name"; | |
excelData[2] = "Country"; | |
for(int i=0;i<1;i++) //This runs once to create 1 row | |
{ | |
myRow = mySheet.createRow(i); | |
for(int j=0;j<3;j++) //This runs thrice to create and set column values to 'excelData" | |
{ | |
myCell = myRow.createCell(j); | |
myCell.setCellValue(excelData[j]); | |
} | |
} | |
String directoryName = "temp"; | |
File file = new File(directoryName); | |
if (!file.exists()) | |
{ | |
if (file.mkdir()) | |
{ | |
System.out.println("Directory is created!"); | |
} | |
else | |
{ | |
System.out.println("Failed to create directory!"); | |
} | |
} | |
else | |
{ | |
System.out.println("Directory already exists!"); | |
} | |
String fileSeparator = System.getProperty("file.separator"); | |
String relativePath = directoryName+fileSeparator+"MyExcelFile.xlsx"; | |
System.out.println("relativePath:"+relativePath); | |
file = new File(relativePath); | |
FileOutputStream out = new FileOutputStream(file); | |
myWorkBook.write(out); | |
myWorkBook.close(); | |
out.close(); | |
String filePath = file.getAbsolutePath(); | |
System.out.println("Excel File created:"+filePath); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment