用jxl如何实现在已有的excel表中添加数据

用jxl如何实现在已有的excel表中添加数据,第1张

工具-宏中的VB编辑器,插入-模块。复制下列程序

Sub test()

Dim r As Range

For Each r In ActiveSheet.UsedRange

r = "jxl" &r

Next

End Sub

关闭VB编辑器,在任一需要加单引号的工作表上,点工具-宏,找到test,点执行即可。

这个功能靠jxl是做不到的,下面是api的部分方法,你可以看看:(有点乱,你可以到网上下一个完整的)

setAlignment(Alignment) - Method in class jxl.write.WritableCellFormat

Sets the horizontal alignment for this format

setArrayGrowSize(int) - Method in class jxl.WorkbookSettings

Sets the amount of memory by which to increase the amount of memory allocated to storing the workbook data.

setAutoFilterDisabled(boolean) - Method in class jxl.WorkbookSettings

Sets the autofilter disabled

setAutomaticFormulaCalculation(boolean) - Method in class jxl.SheetSettings

Sets the automatic formula calculation flag

setAutosize(boolean) - Method in class jxl.CellView

Sets the autosize flag.

setBackground(Colour) - Method in class jxl.write.WritableCellFormat

Sets the background colour for this cell format

setBackground(Colour, Pattern) - Method in class jxl.write.WritableCellFormat

Sets the background colour and pattern for this cell format

setBoldStyle(WritableFont.BoldStyle) - Method in class jxl.write.WritableFont

Sets the bold style for this font, if the font hasn't been initialized

setBorder(Border, BorderLineStyle) - Method in class jxl.write.WritableCellFormat

Sets the specified border for this format

setBorder(Border, BorderLineStyle, Colour) - Method in class jxl.write.WritableCellFormat

Sets the specified border for this format

setBottomMargin(double) - Method in class jxl.SheetSettings

Sets the bottom margin

setCellFeatures(WritableCellFeatures) - Method in interface jxl.write.WritableCell

Sets the cell features

setCellFormat(CellFormat) - Method in interface jxl.write.WritableCell

Sets the cell format for this cell

setCellValidationDisabled(boolean) - Method in class jxl.WorkbookSettings

Sets the ignore cell validation flag

setCharacterSet(int) - Method in class jxl.WorkbookSettings

Sets the character set.

setColour(Colour) - Method in class jxl.write.WritableFont

Sets the colour for this font, if the font hasn't been initialized

setColourRGB(Colour, int, int, int) - Method in class jxl.write.WritableWorkbook

Sets the RGB value for the specified colour for this workbook

setColumn(double) - Method in class jxl.write.WritableImage

Accessor for the image position

setColumnGroup(int, int, boolean) - Method in interface jxl.write.WritableSheet

Sets a column grouping

setColumnView(int, int) - Method in interface jxl.write.WritableSheet

Sets the width of the column on this sheet, in characters.

setColumnView(int, int, CellFormat) - Method in interface jxl.write.WritableSheet

Deprecated. Use the CellView bean instead

setColumnView(int, CellView) - Method in interface jxl.write.WritableSheet

Sets the view for this column

setComment(String) - Method in class jxl.write.WritableCellFeatures

Sets the cell comment

setComment(String, double, double) - Method in class jxl.write.WritableCellFeatures

Sets the cell comment and sets the size of the text box (in cells) in which the comment is displayed

setCopies(int) - Method in class jxl.SheetSettings

Sets the number of copies

setDataValidationList(Collection) - Method in class jxl.write.WritableCellFeatures

The list of items to validate for this cell.

setDataValidationRange(int, int, int, int) - Method in class jxl.write.WritableCellFeatures

The list of items to validate for this cell in the form of a cell range.

setDataValidationRange(String) - Method in class jxl.write.WritableCellFeatures

Sets the data validation based upon a named range

setDate(Date) - Method in class jxl.write.DateTime

Sets the date for this cell

setDate(Date, DateRecord.GMTDate) - Method in class jxl.write.DateTime

Sets the date for this cell, performing the necessary timezone adjustments

setDefaultColumnWidth(int) - Method in class jxl.SheetSettings

Sets the default column width

setDefaultRowHeight(int) - Method in class jxl.SheetSettings

Sets the default row height

setDescription(String) - Method in class jxl.write.WritableHyperlink

Sets the description to appear in the hyperlink cell

setDimension(int) - Method in class jxl.CellView

Deprecated. use the setSize method instead

setDisplayZeroValues(boolean) - Method in class jxl.SheetSettings

Sets the displayZeroValues property

setDrawingsDisabled(boolean) - Method in class jxl.WorkbookSettings

Disables the handling of drawings

setEncoding(String) - Method in class jxl.WorkbookSettings

Sets the encoding for this workbook

setExcel9File(boolean) - Method in class jxl.WorkbookSettings

setExcelDisplayLanguage(String) - Method in class jxl.WorkbookSettings

Sets the language in which the generated file will display

setExcelRegionalSettings(String) - Method in class jxl.WorkbookSettings

Sets the regional settings for the generated excel file

setFile(File) - Method in class jxl.write.WritableHyperlink

Sets the file activated by this hyperlink

setFitHeight(int) - Method in class jxl.SheetSettings

Sets the number of pages vertically that this sheet will be printed into

setFitToPages(boolean) - Method in class jxl.SheetSettings

Accessor for the fit to pages flag

setFitWidth(int) - Method in class jxl.SheetSettings

Sets the number of pages widthwise which this sheet should be printed into

setFontName(String) - Method in class jxl.HeaderFooter.Contents

Sets the font of text subsequently appended to this object..

setFontSize(int) - Method in class jxl.HeaderFooter.Contents

Sets the font size of text subsequently appended to this object.

setFooter(HeaderFooter) - Method in class jxl.SheetSettings

Sets the footer

setFooter(String, String, String) - Method in interface jxl.write.WritableSheet

Deprecated. use the SheetSettings bean

setFooterMargin(double) - Method in class jxl.SheetSettings

Sets the margin for any page footer

setFormat(CellFormat) - Method in class jxl.CellView

Sets the cell format for this group of cells

setFormulaAdjust(boolean) - Method in class jxl.WorkbookSettings

Setter for the formula adjust disabled property

setGCDisabled(boolean) - Method in class jxl.WorkbookSettings

Sets the garbage collection disabled

setHeader(HeaderFooter) - Method in class jxl.SheetSettings

Sets the header

setHeader(String, String, String) - Method in interface jxl.write.WritableSheet

Deprecated. use the SheetSettings bean

setHeaderMargin(double) - Method in class jxl.SheetSettings

Sets the margin for any page headers

setHeight(double) - Method in class jxl.write.WritableImage

Accessor for the image dimensions Note that the actual size of the rendered image will depend on the height of the rows it spans

setHidden(boolean) - Method in class jxl.CellView

Sets the hidden status of this row/column

setHidden(boolean) - Method in class jxl.SheetSettings

Sets the hidden status of this worksheet

setHidden(boolean) - Method in interface jxl.write.WritableSheet

Deprecated. use the SheetSettings bean instead

setHideobj(int) - Method in class jxl.WorkbookSettings

The HIDEOBJ record stores options selected in the Options dialog,View tab Possible values are: HIDEOBJ_HIDE_ALL, HIDEOBJ_SHOW_ALL and HIDEOBJ_SHOW_PLACEHOLDERS

setHorizontalCentre(boolean) - Method in class jxl.SheetSettings

Sets the horizontal centre

setHorizontalFreeze(int) - Method in class jxl.SheetSettings

Sets the row at which the pane is frozen horizontally

setHorizontalPrintResolution(int) - Method in class jxl.SheetSettings

Sets the horizontal print resolution

setIgnoreBlanks(boolean) - Method in class jxl.WorkbookSettings

Sets the ignore blanks flag

setImageAnchor(Drawing.ImageAnchorProperties) - Method in class jxl.write.WritableImage

Accessor for the anchor properties

setIndentation(int) - Method in class jxl.write.WritableCellFormat

Sets the indentation of the cell text

setInitialFileSize(int) - Method in class jxl.WorkbookSettings

Sets the initial amount of memory allocated to store the workbook data when reading a worksheet.

setItalic(boolean) - Method in class jxl.write.WritableFont

Sets the italic indicator for this font, if the font hasn't been initialized

setLeftMargin(double) - Method in class jxl.SheetSettings

Sets the left margin

setLocale(Locale) - Method in class jxl.WorkbookSettings

Sets the locale used by JExcelApi to generate the spreadsheet.

setLocation(String, WritableSheet, int, int, int, int) - Method in class jxl.write.WritableHyperlink

Sets the location of the cells to be linked to within this workbook

setLocked(boolean) - Method in class jxl.write.WritableCellFormat

Sets whether or not this XF record locks the cell.

setMergedCellChecking(boolean) - Method in class jxl.WorkbookSettings

Accessor to set the merged cell checking

setName(String) - Method in interface jxl.write.WritableSheet

Sets the name of this sheet

setNamesDisabled(boolean) - Method in class jxl.WorkbookSettings

Disables the handling of names

setNormalMagnification(int) - Method in class jxl.SheetSettings

Accessor for the normal magnificaton factor.

setNumberValidation(double, BaseCellFeatures.ValidationCondition) - Method in class jxl.write.WritableCellFeatures

Sets the numeric value against which to validate

setNumberValidation(double, double, BaseCellFeatures.ValidationCondition) - Method in class jxl.write.WritableCellFeatures

Sets the numeric range against which to validate the data

JXL(Java Excel API)是一个用来动态读写Excel文件的开源框架,利用它可以在任何支持Java的 *** 作系统上动态读写Excel文件。JXL的主页是:http://www.andykhan.com/jexcelapi/,可以在这里下载到它的最新的版本。

你可以分别通过如下命令

java -jar jxl.jar -xml test.xls

java -jar jxl.jar -cvs test.xls

以xml和cvs格式查看test.xls,这是因为JXL作者实现了一个用来演示的jxl.demo.Demo的类。

当然我们使用JXL主要是用它来动态读写Excel文件。现在就来看看JXL在对Excel的读和写上都提供了那些支持。

先来看看Excel文件中都有写上面对象

1 文件对象 2工作簿对象 3 单元格对象。

相应的在JXL中就有Workbook,Sheet ,Cell 。通过这三个对象我们就可以实现Excel文件的读取工作。

我们先想想一下读取步骤,不管是什么样的Excel *** 作框架必定都要经历

1选取Excel文件,2选择工作簿,3选择Cell,4读取信息。

那么现在就可以看看JXL中这四步骤如何体现:

//通过Workbook的静态方法getWorkbook选取Excel文件

Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"))

//通过Workbook的getSheet方法选择第一个工作簿(从0开始)

Sheet sheet = workbook.getSheet(0)

//通过Sheet方法的getCell方法选择位置为C2的单元格(两个参数都从0开始)

Cell c2 = sheet.getCell(2,1)

//通过Cell的getContents方法把单元格中的信息以字符的形式读取出来

String stringc2 = c2.getContents()

可以看到正是刚才所说的四个步骤。

我们都知道Excel单元格是有格式的,那么这些信息如何取得。

Cell提供了一个getType方法能够返回单元格的类型信息,同时JXL提供了一个CellType类用来预设Excel中的类型信息,而且JXL提供了一些Cell类的子类用来分别用来表示各种类型的单元格,如LabelCell,NumberCell,DateCell分别表示字符、数值、日期类型的单元格。所以我们可以这样写:

if (c2.getType() == CellType.NUMBER)

{

NumberCell nc = (NumberCell) c2

numberb2 = nc.getValue()

}

最后不要忘记关闭workbook以释放资源:

workbook.close()

现在总结一下:三个对象,四个步骤,注意格式。就可以轻松的从Excel文件中读取数据。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11474830.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存