【无标题】用记事本编写的java代码,实现EXCEL中按固定列将一个表格拆分成多个表格

这篇博客分享了一段使用记事本编写的Java代码,能够将一个EXCEL表格根据特定列拆分成多个表格。内容包含了主要的实现模块,并提及了辅助功能如界面设计、EXCEL导入和过滤等。作者计划为有需求的读者提供更多的相关更新。
摘要由CSDN通过智能技术生成

用记事本编写的java代码,实现EXCEL中按固定列将一个表格拆分成多个表格,部分内容是从网络中搜索获得、仅供学些交流。这个是拆分的主要实现模块,还有几个辅助模块,包括界面设计,EXCEL表导入、过滤等功能,有需要的朋友我会继续更新。

//导入apache.poi的相关类
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;

import java.util.Vector;
import java.util.Iterator;

/**
 * 提供常用excel操作<br>
 * 大部分代码从网上搜索,后续根据需求修改
 * 
 */
 
 public class ExcelOperationUtil{
	//设置三个常量分别标记EXCEL的03版、07版和打开错误
	public static final int EXCEL_VERSION_XLS = 0;//03版
	public static final int EXCEL_VERSION_XLSX = 1;//07版
	public static final int EXCEL_VERSION_ERROR = 2;//打开文件错误
	
	
	public static int getExcelVersion(String fileName){
		if(fileName.endsWith(".xls")){
			return EXCEL_VERSION_XLS;
		}else if(fileName.endsWith(".xlsx")){
			return EXCEL_VERSION_XLSX;
		}else{
			return EXCEL_VERSION_ERROR;
		}
	}
	
	public static Workbook getWorkbook(File file,int excelVersion){
		Workbook workBook = null;
		InputStream inputStream = null;
        try{
            if(file!=null){
				inputStream = new FileInputStream(file);
			}else{
				return null;
			}
			
			/*
			*以下两种方式都可以生成Workbook,方式1兼容性可能更强
			*以下两种方式生成Workbool均不能导入带批注的EXCEL文件,需要修改代码,暂时未找到解决办法
			*/
			//方式1生成Workbook
			try{
				workBook = WorkbookFactory.create(inputStream);
			}catch(java.lang.Exception e){
				e.printStackTrace();
				return null;
			}
			//方式2生成Workbook
            /*if(excelVersion == EXCEL_VERSION_XLS){
				try{
					workBook = new HSSFWorkbook(inputStream);
				}catch(IOException e){
					return null;
				}
            }else{
				try{
					workBook = new XSSFWorkbook(inputStream);
				}catch(java.lang.Exception e){
					e.printStackTrace();
					return null;
				}
            }
			*/
            inputStream.close();
		} catch (FileNotFoundException e) {
            e.printStackTrace();
			return null;
        } catch (IOException e) {
            e.printStackTrace();
			return null;
        }finally {
            if (null != inputStream){
                try{
                    inputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
					return null;
                }
            }
		}
		return workBook;
	}
	
	//此静态函数返回WorkBook的所有Sheet页的名称,尽量用Vector集合存放Sheet页的名称,如果用数组返回null可能有问题
	public static Vector<String> getSheetNames(Workbook workBook){
		int sheetNumber = 0;
		if(workBook!=null){
			sheetNumber = workBook.getNumberOfSheets();
		}else{
			return null;
		}
		Vector<String> sheetNames = new Vector<String>();
		for(int i = 0;i< sheetNumber;i++){
			sheetNames.add(workBook.getSheetName(i));
		}
		return sheetNames;
	}
	
	public static int getRowCount(Sheet sheet){
		if(null != sheet){
			return sheet.getLastRowNum()+1;//getLastRowNum最后一行的下表索引(从0开
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以帮你回答这个问题。 首先,要实现记事本程序,需要使用Java的图形化界面工具包,比如Swing或JavaFX。这里我以Swing为例。 下面是一个简单的记事本程序的代码,其包括了切换字体和颜色的功能: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyNotepad extends JFrame implements ActionListener { private JTextArea textArea; private JMenuBar menuBar; private JMenu fileMenu, editMenu, formatMenu; private JMenuItem newMenuItem, openMenuItem, saveMenuItem, exitMenuItem; private JMenuItem cutMenuItem, copyMenuItem, pasteMenuItem; private JMenuItem fontMenuItem, colorMenuItem; public MyNotepad() { setTitle("记事本"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane); menuBar = new JMenuBar(); fileMenu = new JMenu("文件"); editMenu = new JMenu("编辑"); formatMenu = new JMenu("格式"); newMenuItem = new JMenuItem("新建"); openMenuItem = new JMenuItem("打开"); saveMenuItem = new JMenuItem("保存"); exitMenuItem = new JMenuItem("退出"); cutMenuItem = new JMenuItem("剪切"); copyMenuItem = new JMenuItem("复制"); pasteMenuItem = new JMenuItem("粘贴"); fontMenuItem = new JMenuItem("字体"); colorMenuItem = new JMenuItem("颜色"); newMenuItem.addActionListener(this); openMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); exitMenuItem.addActionListener(this); cutMenuItem.addActionListener(this); copyMenuItem.addActionListener(this); pasteMenuItem.addActionListener(this); fontMenuItem.addActionListener(this); colorMenuItem.addActionListener(this); fileMenu.add(newMenuItem); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); editMenu.add(cutMenuItem); editMenu.add(copyMenuItem); editMenu.add(pasteMenuItem); formatMenu.add(fontMenuItem); formatMenu.add(colorMenuItem); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(formatMenu); setJMenuBar(menuBar); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == newMenuItem) { textArea.setText(""); } else if (e.getSource() == openMenuItem) { // 打开文件 } else if (e.getSource() == saveMenuItem) { // 保存文件 } else if (e.getSource() == exitMenuItem) { System.exit(0); } else if (e.getSource() == cutMenuItem) { textArea.cut(); } else if (e.getSource() == copyMenuItem) { textArea.copy(); } else if (e.getSource() == pasteMenuItem) { textArea.paste(); } else if (e.getSource() == fontMenuItem) { Font font = JFontChooser.showDialog(this, "选择字体", textArea.getFont()); if (font != null) { textArea.setFont(font); } } else if (e.getSource() == colorMenuItem) { Color color = JColorChooser.showDialog(this, "选择颜色", textArea.getForeground()); if (color != null) { textArea.setForeground(color); } } } public static void main(String[] args) { new MyNotepad(); } } ``` 这个程序包括了菜单栏、文本区和一些菜单项。其,字体和颜色的功能是通过JFontChooser和JColorChooser实现的。 你可以运行这个程序并尝试切换字体和颜色。如果你想要添加更多的功能,可以在actionPerformed方法添加相应的代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Captain010110

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值