⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xlswriter.java

📁 JAVA EXCEL操作API
💻 JAVA
字号:
/*
 * Created on 2004-3-24
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.zosatapo.xls.io;

import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.zosatapo.xls.core.Cell;
import com.zosatapo.xls.core.Column;
import com.zosatapo.xls.core.CoreException;
import com.zosatapo.xls.core.Record;
import com.zosatapo.xls.core.Schema;
import com.zosatapo.xls.util.IoUtils;

/**
 * @author zosatapo
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class XlsWriter implements Writer
{
  private Schema schema;
  private OutputStream fout;
  
  HSSFWorkbook workBook;
  HSSFSheet    sheet;
  
  int rowCount;
  
  public XlsWriter(Schema schema_,OutputStream fout_)
  {
    this.schema=schema_;
    this.fout=fout_;
    
    this.workBook= new HSSFWorkbook();
    this.sheet= workBook.createSheet();
    
    this.rowCount=0;
  }
	
  public void write(Record record) throws CoreException
	{    
    HSSFRow row = sheet.createRow(rowCount);
    
    int sizeCell=record.getCellCount();
    Cell cellObject=null;
    
    int cellIndex=0;
    
    for(int i=0;i<sizeCell;++i)
    {
      cellObject=record.getCell(i);
      if(!cellObject.isNull())
      {
        IoUtils.writeCell(workBook,row,cellIndex,cellObject);
        ++cellIndex;
      }
    }
    
    ++rowCount;
    
    System.err.println("[Write]{" + record + "}");
	}

	public void close() throws CoreException
	{
    try
    {
      workBook.write(fout);
    }
    catch(Exception ex)
    {
      throw new CoreException(ex);
    }
  }
  
  public void writeTitle() 
  throws CoreException
  {
    HSSFRow row = sheet.createRow(rowCount);
    
    int sizeColumn=schema.getColumnCount();
    Column column=null;
    
    int titleIndex=0;
    for(int i=0;i<sizeColumn;++i)
    {
      column=schema.getColumn(i);
      
      if(!column.isNull())
      {
        IoUtils.writeTitle(row,titleIndex,column.getName());
        ++titleIndex;
      }
    }
    
    ++rowCount;
  }
  
	public Schema getSchema()
	{
		return schema;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -