Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java apache POI MS Office Excel无法打开字体颜色集Excel文件_Java_Excel_Apache Poi_Ms Office - Fatal编程技术网

Java apache POI MS Office Excel无法打开字体颜色集Excel文件

Java apache POI MS Office Excel无法打开字体颜色集Excel文件,java,excel,apache-poi,ms-office,Java,Excel,Apache Poi,Ms Office,我在apache poi中遇到了一个奇怪的问题 我正在使用ApachePOI3.17创建一个Excel文件。如果我设置字体颜色,则生成的excel文件不会使用最新的MS Office excel viewer打开,而是使用Libre Office和Mac OS Numbers应用程序打开 但如果我注释掉“urlFont.setColor((short)Color.BLACK.getRGB())”行,则生成的文件将使用Ms Office Excel viewer打开(其他应用程序也会打开) 有人面

我在apache poi中遇到了一个奇怪的问题

我正在使用ApachePOI3.17创建一个Excel文件。如果我设置字体颜色,则生成的excel文件不会使用最新的MS Office excel viewer打开,而是使用Libre Office和Mac OS Numbers应用程序打开

但如果我注释掉“urlFont.setColor((short)Color.BLACK.getRGB())”行,则生成的文件将使用Ms Office Excel viewer打开(其他应用程序也会打开)

有人面对过这个问题吗

 import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.xssf.usermodel.*;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class WriteExcelBasic {
    public static void main(String[] args) throws IOException {

        String excelFileName = "/Users/home/Test3.xlsx";
        FileOutputStream fos = new FileOutputStream(excelFileName);


        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle style = wb.createCellStyle();

        XSSFSheet sheet = wb.createSheet("sheet");
        Font urlFont = wb.createFont();
        urlFont.setFontHeight((short)(9*20));
       // urlFont.setUnderline((byte)10);
        //urlFont.setBold(true);
       urlFont.setFontName("Arial");
        urlFont.setItalic(true);
      urlFont.setColor((short)Color.BLACK.getRGB());// commenting out this line will work



        style.setFont(urlFont);
        for (int r = 0; r < 3; r++) {
            XSSFRow row = sheet.createRow(r);


            for (int c = 0; c < 3; c++) {
                XSSFCell cell = row.createCell(c);


                Hyperlink link = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
                String ss = "http://news.google.com/news/headlines?ned=us&hl=en";
                //String ss = "swasdqde";
               link.setAddress(ss);
                cell.setHyperlink(link);
                cell.setCellValue(ss);
                if(r == 1) {
                    System.out.println("In yellow");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.YELLOW));
                } else {
                    System.out.println("In red");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.RED));
                }
                cell.setCellStyle(style);


            }
        }

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            wb.write(baos);
            byte[] myByteArray = baos.toByteArray();
            fos.write(myByteArray);
            fos.flush();
        }
        finally {
            wb.close();
            fos.close();
        }
    }
}
import org.apache.poi.common.usermodel.HyperlinkType;
导入org.apache.poi.ss.usermodel.FillPatternType;
导入org.apache.poi.ss.usermodel.Font;
导入org.apache.poi.ss.usermodel.Hyperlink;
导入org.apache.poi.xssf.usermodel.*;
导入java.awt.*;
导入java.io.ByteArrayOutputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
公共类WriteExcelBasic{
公共静态void main(字符串[]args)引发IOException{
字符串excelFileName=“/Users/home/Test3.xlsx”;
FileOutputStream fos=新的FileOutputStream(excelFileName);
XSSF工作簿wb=新XSSF工作簿();
XSSFCellStyle=wb.createCellStyle();
XSSFSheet sheet=wb.createSheet(“sheet”);
Font urlFont=wb.createFont();
urlFont.setFontHeight((短)(9*20));
//urlFont.setUnderline((字节)10);
//urlFont.setBold(true);
urlFont.setFontName(“Arial”);
urlFont.setItalic(true);
urlFont.setColor((短)Color.BLACK.getRGB());//注释掉这一行就行了
style.setFont(urlFont);
对于(int r=0;r<3;r++){
XSSFRow row=sheet.createRow(r);
对于(int c=0;c<3;c++){
XSSFCell cell=row.createCell(c);
Hyperlink=wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
字符串ss=”http://news.google.com/news/headlines?ned=us&hl=en";
//字符串ss=“swasdqde”;
链接设置地址(ss);
cell.setHyperlink(link);
cell.setCellValue(ss);
如果(r==1){
System.out.println(“黄色”);
style.setFillPattern(FillPatternType.SOLID\u前景);
style.setFillForegroundColor(新XSSFColor(Color.YELLOW));
}否则{
System.out.println(“红色”);
style.setFillPattern(FillPatternType.SOLID\u前景);
style.setFillForegroundColor(新XSSFColor(Color.RED));
}
cell.setCellStyle(style);
}
}
try(ByteArrayOutputStream=newbytearrayoutputstream()){
wb.write(baos);
字节[]myByteArray=baos.toByteArray();
fos.write(myByteArray);
fos.flush();
}
最后{
wb.close();
fos.close();
}
}
}

我将字体更改为XSFFFont,并在该字体中使用XSSFCOLOR,并且它可以正常工作

import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.xssf.usermodel.*;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class WriteExcelBasic {
    public static void main(String[] args) throws IOException {

        String excelFileName = "/Users/home/Test3.xlsx";
        FileOutputStream fos = new FileOutputStream(excelFileName);


        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle style = wb.createCellStyle();

        XSSFSheet sheet = wb.createSheet("sheet");
 XSSFFont urlFont = wb.createFont();// changed lines
 urlFont.setFontHeight((short)(9*20));
       // urlFont.setUnderline((byte)10);
        //urlFont.setBold(true);
       urlFont.setFontName("Arial");
        urlFont.setItalic(true);
      urlFont.setColor(new XSSFColor(Color.BLUE)); // changed lines



        style.setFont(urlFont);
        for (int r = 0; r < 3; r++) {
            XSSFRow row = sheet.createRow(r);


            for (int c = 0; c < 3; c++) {
                XSSFCell cell = row.createCell(c);


                Hyperlink link = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
                String ss = "http://news.google.com/news/headlines?ned=us&hl=en";
                //String ss = "swasdqde";
               link.setAddress(ss);
                cell.setHyperlink(link);
                cell.setCellValue(ss);
                if(r == 1) {
                    System.out.println("In yellow");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.YELLOW));
                } else {
                    System.out.println("In red");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.RED));
                }
                cell.setCellStyle(style);


            }
        }

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            wb.write(baos);
            byte[] myByteArray = baos.toByteArray();
            fos.write(myByteArray);
            fos.flush();
        }
        finally {
            wb.close();
            fos.close();
        }
    }
}
import org.apache.poi.common.usermodel.HyperlinkType;
导入org.apache.poi.ss.usermodel.FillPatternType;
导入org.apache.poi.ss.usermodel.Font;
导入org.apache.poi.ss.usermodel.Hyperlink;
导入org.apache.poi.xssf.usermodel.*;
导入java.awt.*;
导入java.io.ByteArrayOutputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
公共类WriteExcelBasic{
公共静态void main(字符串[]args)引发IOException{
字符串excelFileName=“/Users/home/Test3.xlsx”;
FileOutputStream fos=新的FileOutputStream(excelFileName);
XSSF工作簿wb=新XSSF工作簿();
XSSFCellStyle=wb.createCellStyle();
XSSFSheet sheet=wb.createSheet(“sheet”);
XSSFFont urlFont=wb.createFont();//更改的行
urlFont.setFontHeight((短)(9*20));
//urlFont.setUnderline((字节)10);
//urlFont.setBold(true);
urlFont.setFontName(“Arial”);
urlFont.setItalic(true);
urlFont.setColor(新XSSFColor(Color.BLUE));//更改的行
style.setFont(urlFont);
对于(int r=0;r<3;r++){
XSSFRow row=sheet.createRow(r);
对于(int c=0;c<3;c++){
XSSFCell cell=row.createCell(c);
Hyperlink=wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
字符串ss=”http://news.google.com/news/headlines?ned=us&hl=en";
//字符串ss=“swasdqde”;
链接设置地址(ss);
cell.setHyperlink(link);
cell.setCellValue(ss);
如果(r==1){
System.out.println(“黄色”);
style.setFillPattern(FillPatternType.SOLID\u前景);
style.setFillForegroundColor(新XSSFColor(Color.YELLOW));
}否则{
System.out.println(“红色”);
style.setFillPattern(FillPatternType.SOLID\u前景);
style.setFillForegroundColor(新XSSFColor(Color.RED));
}
cell.setCellStyle(style);
}
}
try(ByteArrayOutputStream=newbytearrayoutputstream()){
wb.write(baos);
字节[]myByteArray=baos.toByteArray();
fos.write(myByteArray);
fos.flush();
}
最后{
wb.close();
fos.close();
}
}
}

我将字体更改为XSFFFont,并在该字体中使用XSSFCOLOR,并且它可以正常工作

import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.xssf.usermodel.*;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class WriteExcelBasic {
    public static void main(String[] args) throws IOException {

        String excelFileName = "/Users/home/Test3.xlsx";
        FileOutputStream fos = new FileOutputStream(excelFileName);


        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle style = wb.createCellStyle();

        XSSFSheet sheet = wb.createSheet("sheet");
 XSSFFont urlFont = wb.createFont();// changed lines
 urlFont.setFontHeight((short)(9*20));
       // urlFont.setUnderline((byte)10);
        //urlFont.setBold(true);
       urlFont.setFontName("Arial");
        urlFont.setItalic(true);
      urlFont.setColor(new XSSFColor(Color.BLUE)); // changed lines



        style.setFont(urlFont);
        for (int r = 0; r < 3; r++) {
            XSSFRow row = sheet.createRow(r);


            for (int c = 0; c < 3; c++) {
                XSSFCell cell = row.createCell(c);


                Hyperlink link = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
                String ss = "http://news.google.com/news/headlines?ned=us&hl=en";
                //String ss = "swasdqde";
               link.setAddress(ss);
                cell.setHyperlink(link);
                cell.setCellValue(ss);
                if(r == 1) {
                    System.out.println("In yellow");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.YELLOW));
                } else {
                    System.out.println("In red");
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(new XSSFColor(Color.RED));
                }
                cell.setCellStyle(style);


            }
        }

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            wb.write(baos);
            byte[] myByteArray = baos.toByteArray();
            fos.write(myByteArray);
            fos.flush();
        }
        finally {
            wb.close();
            fos.close();
        }
    }
}
import org.apache.poi.common.usermodel.HyperlinkType;
导入org.apache.poi.ss.usermodel.FillPatternType;
导入org.apache.poi.ss.usermodel.Font;
导入org.apache.poi.ss.usermodel.Hyperlink;
导入org.apache.poi.xssf.usermodel.*;
导入java.awt.*;
导入java.io.ByteArrayOutputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
公共类WriteExcelB