JAVA读写EXCEL

JAVA读写EXCEL


本文主要向你演示如何使用JavaExcel API来读写Excel文件。关于JavaExcel API,这是一个开源的lib库。其相关的feature如下:

  • 支持Excel 95, 97, 2000, XP, 2003 的制表页。
  • 可以读写相关的Excel公式 (仅支持Excel 97 及以后版本)
  • 可以生成 Excel 2000 格式的xls文件。
  • 支持字体,数字和日期格式。
  • 支持单元格的阴影,边框和颜色。
  • 可以修改已存在的制表页。
  • 国际化多语言集。(公式目前支持,英文,法文,西班牙文和德文)
  • 支持图表拷贝。
  • 支持图片的插入和复制。
  • 日志生成可以使用Jakarta Commons Logging, log4j, JDK 1.4 Logger, 等。
  • 更多……
  • 你可以在这里下载:http://jexcelapi.sourceforge.net/,然后,把jxl.jar加到你的Java的classpath中。
  • 下面是两段例程,一段是如何创建Excel,一段是如何读取Excel。
  • 创建Excel
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    package writer;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.Locale;
     
    import jxl.CellView;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.format.UnderlineStyle;
    import jxl.write.Formula;
    import jxl.write.Label;
    import jxl.write.Number;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;
     
     
    public class WriteExcel {
     
         private WritableCellFormat timesBoldUnderline;
         private WritableCellFormat times;
         private String inputFile;
         
    public void setOutputFile(String inputFile) {
         this .inputFile = inputFile;
         }
     
         public void write() throws IOException, WriteException {
             File file = new File(inputFile);
             WorkbookSettings wbSettings = new WorkbookSettings();
     
             wbSettings.setLocale( new Locale( "en" , "EN" ));
     
             WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
             workbook.createSheet( "Report" , 0 );
             WritableSheet excelSheet = workbook.getSheet( 0 );
             createLabel(excelSheet);
             createContent(excelSheet);
     
             workbook.write();
             workbook.close();
         }
     
         private void createLabel(WritableSheet sheet)
                 throws WriteException {
             // Lets create a times font
             WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10 );
             // Define the cell format
             times = new WritableCellFormat(times10pt);
             // Lets automatically wrap the cells
             times.setWrap( true );
     
             // Create create a bold font with unterlines
             WritableFont times10ptBoldUnderline = new WritableFont(
                     WritableFont.TIMES, 10 , WritableFont.BOLD, false ,
                     UnderlineStyle.SINGLE);
             timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
             // Lets automatically wrap the cells
             timesBoldUnderline.setWrap( true );
     
             CellView cv = new CellView();
             cv.setFormat(times);
             cv.setFormat(timesBoldUnderline);
             cv.setAutosize( true );
     
             // Write a few headers
             addCaption(sheet, 0 , 0 , "Header 1" );
             addCaption(sheet, 1 , 0 , "This is another header" );
             
     
         }
     
         private void createContent(WritableSheet sheet) throws WriteException,
                 RowsExceededException {
             // Write a few number
             for ( int i = 1 ; i < 10 ; i++) {
                 // First column
                 addNumber(sheet, 0 , i, i + 10 );
                 // Second column
                 addNumber(sheet, 1 , i, i * i);
             }
             // Lets calculate the sum of it
             StringBuffer buf = new StringBuffer();
             buf.append( "SUM(A2:A10)" );
             Formula f = new Formula( 0 , 10 , buf.toString());
             sheet.addCell(f);
             buf = new StringBuffer();
             buf.append( "SUM(B2:B10)" );
             f = new Formula( 1 , 10 , buf.toString());
             sheet.addCell(f);
     
             // Now a bit of text
             for ( int i = 12 ; i < 20 ; i++) {
                 // First column
                 addLabel(sheet, 0 , i, "Boring text " + i);
                 // Second column
                 addLabel(sheet, 1 , i, "Another text" );
             }
         }
     
         private void addCaption(WritableSheet sheet, int column, int row, String s)
                 throws RowsExceededException, WriteException {
             Label label;
             label = new Label(column, row, s, timesBoldUnderline);
             sheet.addCell(label);
         }
     
         private void addNumber(WritableSheet sheet, int column, int row,
                 Integer integer) throws WriteException, RowsExceededException {
             Number number;
             number = new Number(column, row, integer, times);
             sheet.addCell(number);
         }
     
         private void addLabel(WritableSheet sheet, int column, int row, String s)
                 throws WriteException, RowsExceededException {
             Label label;
             label = new Label(column, row, s, times);
             sheet.addCell(label);
         }
     
         public static void main(String[] args) throws WriteException, IOException {
             WriteExcel test = new WriteExcel();
             test.setOutputFile( "c:/temp/lars.xls" );
             test.write();
             System.out
                     .println( "Please check the result file under c:/temp/lars.xls " );
         }
    }
  • 读取Excel
     
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package reader;
     
    import java.io.File;
    import java.io.IOException;
     
    import jxl.Cell;
    import jxl.CellType;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
     
    public class ReadExcel {
     
         private String inputFile;
     
         public void setInputFile(String inputFile) {
             this .inputFile = inputFile;
         }
     
         public void read() throws IOException  {
             File inputWorkbook = new File(inputFile);
             Workbook w;
             try {
                 w = Workbook.getWorkbook(inputWorkbook);
                 // Get the first sheet
                 Sheet sheet = w.getSheet( 0 );
                 // Loop over first 10 column and lines
     
                 for ( int j = 0 ; j < sheet.getColumns(); j++) {
                     for ( int i = 0 ; i < sheet.getRows(); i++) {
                         Cell cell = sheet.getCell(j, i);
                         CellType type = cell.getType();
                         if (cell.getType() == CellType.LABEL) {
                             System.out.println( "I got a label "
                                     + cell.getContents());
                         }
     
                         if (cell.getType() == CellType.NUMBER) {
                             System.out.println( "I got a number "
                                     + cell.getContents());
                         }
     
                     }
                 }
             } catch (BiffException e) {
                 e.printStackTrace();
             }
         }
     
         public static void main(String[] args) throws IOException {
             ReadExcel test = new ReadExcel();
             test.setInputFile( "c:/temp/lars.xls" );
             test.read();
         }
     
    }
CH341A编程器是一款广泛应用的通用编程设备,尤其在电子工程和嵌入式系统开发领域中,它被用来烧录各种类型的微控制器、存储器和其他IC芯片。这款编程器的最新版本为1.3,它的一个显著特点是增加了对25Q256等32M芯片的支持。 25Q256是一种串行EEPROM(电可擦可编程只存储器)芯片,通常用于存储程序代码、配置数据或其他非易失性信息。32M在这里指的是存储容量,即该芯片可以存储32兆位(Mbit)的数据,换算成字节数就是4MB。这种大容量的存储器在许多嵌入式系统中都有应用,例如汽车电子、工业控制、消费电子设备等。 CH341A编程器的1.3版更新,意味着它可以与更多的芯片型号兼容,特别是针对32M容量的芯片进行了优化,提高了编程效率和稳定性。26系列芯片通常指的是Microchip公司的25系列SPI(串行外围接口)EEPROM产品线,这些芯片广泛应用于各种需要小体积、低功耗和非易失性存储的应用场景。 全功能版的CH341A编程器不仅支持25Q256,还支持其他大容量芯片,这意味着它具有广泛的兼容性,能够满足不同项目的需求。这包括但不限于微控制器、EPROM、EEPROM、闪存、逻辑门电路等多种类型芯片的编程。 使用CH341A编程器进行编程操作时,首先需要将设备通过USB连接到计算机,然后安装相应的驱动程序和编程软件。在本例中,压缩包中的"CH341A_1.30"很可能是编程软件的安装程序。安装后,用户可以通过软件界面选择需要编程的芯片类型,加载待烧录的固件或数据,然后执行编程操作。编程过程中需要注意的是,确保正确设置芯片的电压、时钟频率等参数,以防止损坏芯片。 CH341A编程器1.3版是面向电子爱好者和专业工程师的一款实用工具,其强大的兼容性和易用性使其在众多编程器中脱颖而出。对于需要处理25Q256等32M芯片的项目,或者26系列芯片的编程工作,CH341A编程器是理想的选择。通过持续的软件更新和升级,它保持了与现代电子技术同步,确保用户能方便地对各种芯片进行编程和调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值