I am creating a script for export document as excel. I have found some difficulties in that like: I want cell value like "Name: Mark DOB: 11-11-2014" by merging few cells. Can you please help me to get resolve this ?
我正在为导出文档创建一个作为excel的脚本。我在这方面发现了一些困难:我想要像“Name: Mark DOB: 11-11-2014”这样的单元格值,通过合并几个单元格。你能帮我解决这个问题吗?
Thanks in advance !!
提前谢谢! !
3 个解决方案
#1
8
What you need to do is create a RichTextString for your cell. That's the way of applying different formatting / styles to different parts of the same cell for display in Excel
您需要做的是为您的单元格创建一个RichTextString。这是将不同的格式/样式应用到同一个单元格的不同部分以在Excel中显示的方法
You'll want to review the POI "Working With Rich Text" example for more on how to use it, but broadly it'll be something like
您将需要查看POI“与Rich Text一起工作”的示例,了解如何使用它,但大致上它将是类似的。
Cell cell = row.createCell(1);
RichTextString rt = new XSSFRichTextString("The quick brown fox");
Font font1 = wb.createFont();
font1.setBoldWeight(Font.BOLDWEIGHT_BOLD);
rt.applyFont(0, 10, font1);
Font font2 = wb.createFont();
font2.setItalic(true);
font2.setUnderline(XSSFFont.U_DOUBLE);
rt.applyFont(10, 19, font2);
Font font3 = wb.createFont();
font3.setBoldWeight(Font.BOLDWEIGHT_NORMAL);
rt.append(" Jumped over the lazy dog", font3);
cell.setCellValue(rt);
That should give you a cell with a mixture of bold, italic+underline and normal
这将给您一个单元格,它混合了粗体、斜体+下划线和普通样式
#2
6
I have created a short complete example for this.
我为此创建了一个简短的完整示例。
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
class RichTextTest {
public static void main(String[] args) {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
RichTextString richString = new XSSFRichTextString( "Name: Mark DOB: 11-11-2014" );
//^0 ^4 ^11^14
Font fontBold = wb.createFont();
//fontBold.setBoldweight(Font.BOLDWEIGHT_BOLD);
fontBold.setBold(true);
richString.applyFont( 0, 4, fontBold );
richString.applyFont( 11, 14, fontBold );
cell.setCellValue(richString);
try {
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (FileNotFoundException fnfex) {
} catch (IOException ioex) {
}
}
}
For further reading see documentation.
进一步阅读请参阅文档。
How to create workbook, sheet and cells: http://poi.apache.org/spreadsheet/quick-guide.html#CreateCells
如何创建工作簿、工作表和单元格:http://poi.apache.org/spreadsheet/quick guide.html#CreateCells
How to use Richtext: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRichTextString.html
如何使用Richtext: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRichTextString.html
The Font interface: https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Font.html
字体的接口:https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Font.html
#3
0
Have you tried using JXLS ?
你试过使用JXLS吗?
Using xls templates you can read and write data from Excel. Its very simple to use.
使用xls模板,您可以从Excel中读写数据。使用起来很简单。
#1
8
What you need to do is create a RichTextString for your cell. That's the way of applying different formatting / styles to different parts of the same cell for display in Excel
您需要做的是为您的单元格创建一个RichTextString。这是将不同的格式/样式应用到同一个单元格的不同部分以在Excel中显示的方法
You'll want to review the POI "Working With Rich Text" example for more on how to use it, but broadly it'll be something like
您将需要查看POI“与Rich Text一起工作”的示例,了解如何使用它,但大致上它将是类似的。
Cell cell = row.createCell(1);
RichTextString rt = new XSSFRichTextString("The quick brown fox");
Font font1 = wb.createFont();
font1.setBoldWeight(Font.BOLDWEIGHT_BOLD);
rt.applyFont(0, 10, font1);
Font font2 = wb.createFont();
font2.setItalic(true);
font2.setUnderline(XSSFFont.U_DOUBLE);
rt.applyFont(10, 19, font2);
Font font3 = wb.createFont();
font3.setBoldWeight(Font.BOLDWEIGHT_NORMAL);
rt.append(" Jumped over the lazy dog", font3);
cell.setCellValue(rt);
That should give you a cell with a mixture of bold, italic+underline and normal
这将给您一个单元格,它混合了粗体、斜体+下划线和普通样式
#2
6
I have created a short complete example for this.
我为此创建了一个简短的完整示例。
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
class RichTextTest {
public static void main(String[] args) {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
RichTextString richString = new XSSFRichTextString( "Name: Mark DOB: 11-11-2014" );
//^0 ^4 ^11^14
Font fontBold = wb.createFont();
//fontBold.setBoldweight(Font.BOLDWEIGHT_BOLD);
fontBold.setBold(true);
richString.applyFont( 0, 4, fontBold );
richString.applyFont( 11, 14, fontBold );
cell.setCellValue(richString);
try {
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (FileNotFoundException fnfex) {
} catch (IOException ioex) {
}
}
}
For further reading see documentation.
进一步阅读请参阅文档。
How to create workbook, sheet and cells: http://poi.apache.org/spreadsheet/quick-guide.html#CreateCells
如何创建工作簿、工作表和单元格:http://poi.apache.org/spreadsheet/quick guide.html#CreateCells
How to use Richtext: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRichTextString.html
如何使用Richtext: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRichTextString.html
The Font interface: https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Font.html
字体的接口:https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Font.html
#3
0
Have you tried using JXLS ?
你试过使用JXLS吗?
Using xls templates you can read and write data from Excel. Its very simple to use.
使用xls模板,您可以从Excel中读写数据。使用起来很简单。