gpt4 book ai didi

设计excel文件的java android错误

转载 作者:行者123 更新时间:2023-11-29 23:45:59 25 4
gpt4 key购买 nike

在安卓上写一个程序。在我的程序中,我创建了一个 excel 文件。我的设计 excel 文件有问题。

错误:

Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Color;

代码:

 Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Tets");
Font headerFont;
headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 17);
headerFont.setColor(IndexedColors.RED.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();

在线错误:

CellStyle headerCellStyle = workbook.createCellStyle();

库:

 implementation files('libs/poi-3.17.jar')
implementation files('libs/poi-ooxml-3.17.jar')
implementation files('libs/poi-ooxml-schemas-3.17.jar')

感谢您的帮助。祝你有美好的一天!

最佳答案

我解决了我的问题。这很容易。

  1. 在 Android Studio 上创建一个新项目;

  2. 转到 link ,并下载最新版本poishadow-all.jar;

  3. 这是您必须复制到文件夹中的文件,如下图所示:

image

  1. 转到 File/Project Structure/app/Dependencies/"click to button«green cross»"/Jar Dependency/in folder libs 选择库 (poishadow-all. jar )。点击确定按钮后等待。

  2. 在创建文件的类中,需要插入三行:

    System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl");
    System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
    System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");
  3. 编写代码。

为了体验,您可以使用下一个代码:

public class MainActivity extends AppCompatActivity 
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl");
System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");

try {
writeWorkbook();
} catch (IOException e) {
e.printStackTrace();
}

}

private void writeWorkbook() throws java.io.IOException
{
Workbook wb = new HSSFWorkbook();
try {
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("cell-1");
cell = row.createCell(1);
cell.setCellValue("cell-2");
cell = row.createCell(2);
cell.setCellValue("Hello");

Font headerFont;
headerFont = wb.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 17);
headerFont.setColor(IndexedColors.RED.getIndex());

HSSFCellStyle style = (HSSFCellStyle) wb.createCellStyle();
// style.setFillBackgroundColor(new HSSFColor(new org.apache.poi.java.awt.Color(1, 2, 3)));
style.setFillBackgroundColor((short)17);
style.setFont(headerFont);

Hyperlink link = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
link.setAddress("http://www.google.at");
link.setLabel("Google");
cell.setHyperlink(link);

cell.setCellStyle(style);

sheet.setPrintGridlines(true);

} finally {
wb.close();
}

String excelFileName = "Test.xls"; //name of excel file
FileOutputStream outputStream;

try {
outputStream = openFileOutput(excelFileName, Context.MODE_PRIVATE);
wb.write(outputStream);
// outputStream.write(cell.toString().getBytes());
// outputStream.close();

File file = new File( excelFileName);
file.getAbsolutePath();

FileInputStream f = openFileInput(excelFileName);

} catch (Exception e) {
e.printStackTrace();
}
}
}

所以,我在模拟器上检查程序,并将创建的文件保存在内存中。我可以从设备文件资源管理器打开创建的 excel 文件。方式:File Explorer/data/data/«find your package project»/files - 在这里您可以看到您创建了 excel 文件。

祝你有美好的一天!

关于设计excel文件的java android错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51399039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com