Create Excel file in java using PoI

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

package com.howto;

import java.io.File;

import java.io.FileOutputStream;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

/*

* Here we will learn how to create Excel file and header for the same.

*/

public class CreateExcelFile {

int rownum = 0;

HSSFSheet firstSheet;

Collection<File> files;

HSSFWorkbook workbook;

File exactFile;

{

workbook = new HSSFWorkbook();

firstSheet = workbook.createSheet("FIRST SHEET");

Row headerRow = firstSheet.createRow(rownum);

headerRow.setHeightInPoints(40);

}

public static void main(String args[]) throws Exception {

List<String> headerRow = new ArrayList<String>();

headerRow.add("Employee No");

headerRow.add("Employee Name");

headerRow.add("Employee Address");

List<String> firstRow = new ArrayList<String>();

firstRow.add("1111");

firstRow.add("Gautam");

firstRow.add("India");

List<List> recordToAdd = new ArrayList<List>();

recordToAdd.add(headerRow);

recordToAdd.add(firstRow);

CreateExcelFile cls = new CreateExcelFile(recordToAdd);

cls.createExcelFile();

}

void createExcelFile(){

FileOutputStream fos = null;

try {

fos=new FileOutputStream(new File("ExcelSheet.xls"));

HSSFCellStyle hsfstyle=workbook.createCellStyle();

hsfstyle.setBorderBottom((short) 1);

hsfstyle.setFillBackgroundColor((short)245);

workbook.write(fos);

} catch (Exception e) {

e.printStackTrace();

}

}

CreateExcelFile(List<List> l1) throws Exception {

try {

for (int j = 0; j < l1.size(); j++) {

Row row = firstSheet.createRow(rownum);

List<String> l2= l1.get(j);

for(int k=0; k<l2.size(); k++)

{

Cell cell = row.createCell(k);

cell.setCellValue(l2.get(k));

}

rownum++;

}

} catch (Exception e) {

e.printStackTrace();

} finally {

}

}

}

PoI Jar will be required. Jar can be downloaded from Apache website.

时间: 2024-10-06 05:04:36

Create Excel file in java using PoI的相关文章

Read / Write Excel file in Java using Apache POI

Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year or two ago I was working with finance team where they wanted to pull the credit card transactions for all the customer using various combinations. Ex –

How to Read, Write XLSX File in Java - Apach POI Example---reference

No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used application in software world. Other alternatives like OpenOffice and LiberOffice have failed to take off to challenge MS Office. What this mean to a

[Java]Create Excel File Using Apache POI API

package com.file.properties; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Has

Apache POI – Reading and Writing Excel file in Java

来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, we will discuss about how to read and write an excel file using Apache POI 1. Basic definitions for Apache POI library This section briefly describe a

How to create Excel file in C#

http://csharp.net-informations.com/excel/csharp-create-excel.htm Before you create an Excel file in C# , you have to add the Microsoft Excel 12.0 Object Library to you project.

Read doc file in java using poi

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.howto; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.hwpf.HWPFDocument; /* * Here we will learn how to read a Doc file

java的poi技术写Excel的Sheet

在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-2007,2010] java的poi技术读取Excel数据到MySQL java的jxl技术导入Excel java的poi技术读取和导入Excel 然而,这篇blog主要内容是关于Excel里面怎样去写Sheet数据. 那么在Excel里面什么叫做Sheet呢?如下图红色框里面的内容就是Excel的

Formatting Excel File Using Ole2 In Oracle Forms

Below is the some useful commands of Ole2 to format excel file in Oracle Forms. -- Change font size and name in ole2-- Declare as cfont ole2.obj_type;CFONT := OLE2.GET_OBJ_PROPERTY(CELL, 'Font');OLE2.SET_PROPERTY(CFONT, 'Name','Calibri');       OLE2.

Error: unpack could not create JAR file:

[[email protected] ~]# rpm -ivh jdk-7u55-linux-i586.rpm Preparing...                ########################################### [100%]   1:jdk                    ########################################### [100%]Unpacking JAR files...        rt.jar..