1、新建有Maven的java工程(IDEA中)
File=>New=>Project=>Maven=>Create from archetype 打钩=>next=>GroupId,填写如com.jsq,ArtifactId,填写工程名,如create-xlsx
GroupId和ArtifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
GroupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。
ArtifactId 也是当前工程的名字,定义maven项目在组中唯一的ID
新建后,发现出现了pom.xml和工程名.iml两个文件,设置工程的project structure。
2、使用HSSFWorkbook workbook = new HSSFWorkbook();创建excle文件,所以需要导入这个类所在的库,和在pom.xml添加这个库。
百度HSSFWorkbook 可发现这个类在org.apache.poi.hssf.usermodel.HSSFWorkbook,这个路径下,所以
import org.apache.poi.hssf.usermodel.HSSFWorkbook
并在https://mvnrepository.com/ 中查找查找poi,取使用频率高的或最新的,写入pom.xml中
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
3、创建Excel文件常用的函数
1、HSSFWorkbook workbook = new HSSFWorkbook();//新建一个excel
2、HSSFSheet sheet = workbook.createSheet(“sheet标题”);//新建一个sheet页
3、HSSFRow row = sheet.createRow(index);//新建一行,第index行
4、HSSFCell cell = row.createCell(i);//新建一格,第i列
5、cell.setCellValue(“内容”);//向某个格子中填写内容
6、File file = new File(“xxxx.xlsx”);
workbook.write(file);//新建一个xlsx文件,并将workbook写入这个xlsx文件
7、workbook.close();//关闭
4、设置Excel文件格式常用函数
4.1、设置单元格的颜色
HSSFCellStyle style = workbook.createCellStyle();//新建风格
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);//设置前景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置颜色的填充方式
cell.setCellStyle(style);//运用创建出的风格
系统自带的前景色,参考https://www.cnblogs.com/liaomin416100569/p/9331299.html
4.2、自定义前景色
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//前三行相同
HSSFPalette customPalette = workbook.getCustomPalette();
customPalette.setColorAtIndex(HSSFColor.SKY_BLUE.index, (byte) 0xe0, (byte) 0xf0, (byte) 0xfa);//RGB
cell.setCellStyle(style);
附件:
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class createXlsxTest {
@Test
public void createXlsx() {
String fileDir = "D:/" + "/versionresult/";
String FileName = fileDir + "newxlsx.xlsx";
mkdir(fileDir);
HSSFWorkbook workbook = new HSSFWorkbook();
List<String[]> sheetData = new ArrayList<>();
for (int sheetIndex = 0; sheetIndex < 4; sheetIndex++) {
HSSFSheet sheet = workbook.createSheet(String.valueOf(sheetIndex));
HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFPalette customPalette = workbook.getCustomPalette();
customPalette.setColorAtIndex(HSSFColor.SKY_BLUE.index, (byte) 0xe0,
(byte) 0xf0, (byte) 0xfa);
String[] swVersionTitle = {"1223", "125566", "detail"};
for (int i = 0; i < swVersionTitle.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(swVersionTitle[i]);
cell.setCellStyle(style);
}
sheetData.add(new String[]{"12", "23"});
sheetData.add(new String[]{"23", "354"});
for (int i = 0; i < sheetData.size(); i++) {
HSSFRow row1 = sheet.createRow(i);
String[] neVersion = sheetData.get(i);
for (int j = 0; j < neVersion.length; j++) {
row1.createCell(j).setCellValue(neVersion[j]);
}
}
}
try {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkWrite(FileName);
File file = new File(FileName);
if (file.exists()) {
boolean deleteFlag = file.delete();
}
boolean newFileFlag = file.createNewFile();
workbook.write(file);
System.out.println("成功");
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void mkdir(String fileDir) {
File file = new File(fileDir);
if (!file.exists()) {
boolean result = file.mkdirs();
if (!result) {
}
}
}
}