在java中使用JExcel API编写excel文件时如何设置每列的宽度 [英] How to set the width for each column when write excel file using JExcel API in java
本文介绍了在java中使用JExcel API编写excel文件时如何设置每列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个数据表,我使用JExcel API将它们写入excel文件,
但我的问题是我不知道如何设置excel中每列的宽度。
所以我希望你能帮我解决这个。
提前感谢。
Hi All,
I have a data table and I wrote them to excel file using JExcel API,
but my problem is I don't knowm how to set the width for each column in excel.
So I hope that you will help me to resolve this.
Thank in advance.
推荐答案
import java.io.File;
package com.bethecoder.tutorials.jexcelapi.write;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class CellWidthTest {
/**
* @param args
* @throws IOException
* @throws IOException
* @throws WriteException
* @throws BiffException
*/
public static void main(String[] args) throws IOException, WriteException {
//Creates a writable workbook with the given file name
WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/JXL/CellWidth.xls"));
WritableSheet sheet = workbook.createSheet("My Sheet", 0);
// Create cell font and format
WritableFont cellFont = new WritableFont(WritableFont.TIMES, 12);
cellFont.setColour(Colour.BLUE);
WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
cellFormat.setBackground(Colour.ORANGE);
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
//Set cell width in CHARS
int col = 2;
int widthInChars = 3;
sheet.setColumnView(col, widthInChars);
sheet.addCell(new Label(col, 1, "A", cellFormat));
col = 3;
widthInChars = 4;
sheet.setColumnView(col, widthInChars);
sheet.addCell(new Label(col, 1, "BB", cellFormat));
col = 4;
widthInChars = 16;
sheet.setColumnView(col, widthInChars);
sheet.addCell(new Label(col, 1, "CCCCC", cellFormat));
//Writes out the data held in this workbook in Excel format
workbook.write();
//Close and free allocated memory
workbook.close();
}
}
这篇关于在java中使用JExcel API编写excel文件时如何设置每列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文