电子表格中1900,1904系统和date之间的转化

本文介绍如何在Java中实现1900和1904两种电子表格日期系统的相互转换,并提供了具体的算法实现,包括从序列号到日期的转换以及相反方向的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于1900和1904都早于历元 (即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历),所以需要使用

GregorianCalendar来计算.

其实使用joda计算最方便,但是不能为了这两个计算就引入几百K的jar包.所以自己动手,查了些资料,自己写了出来.

 

	/**
	 * 1900日期系统:电子表格将1900年1月1日保存为序列号2,1900年1月2日保存为序列号3, 1900年1月3日保存为序列号4 ……
	 * 依此类推。<br>
	 * 注意,此计算在秒及其以下单位有误差
	 * 
	 * @param days
	 * @return
	 */
	public static Date getDateFrom1900(double days) {
		GregorianCalendar gc = new GregorianCalendar(1900, 0, 1, 0, 0, 0);
		int day = (int) days;// 小数点前
		gc.add(Calendar.DAY_OF_MONTH, day - 2);
		double digit = days - day;// 小数点后
		if (digit > 0) {
			int ms = (int) (digit * 24 * 60 * 60 * 1000);
			gc.add(Calendar.MILLISECOND, ms);
		}
		return gc.getTime();
	}

	/**
	 * 1904日期系统:电子表格将1904年1月1日保存为序列号0,将1904年1月2 日保存为序列号1,将 1904年1月3日保存为序列号 2 ……
	 * 依此类推。
	 * 
	 * @param days
	 * @return
	 */
	public static Date getDateFrom1904(double days) {
		GregorianCalendar gc = new GregorianCalendar(1904, 0, 1, 0, 0, 0);
		int day = (int) days;// 小数点前
		gc.add(Calendar.DAY_OF_MONTH, day);
		double digit = days - day;// 小数点后
		if (digit > 0) {
			int ms = (int) (digit * 24 * 60 * 60 * 1000);
			gc.add(Calendar.MILLISECOND, ms);
		}
		return gc.getTime();
	}

	public static double dateTo1900(Date date) {
		if (date == null) {
			throw new NullPointerException();
		}
		GregorianCalendar d1900 = new GregorianCalendar(1900, 0, 1, 0, 0, 0);
		GregorianCalendar dcurr = new GregorianCalendar();
		dcurr.setTime(date);
		int days = getDaysBetween2Calendar(d1900, dcurr);
		int hours = dcurr.get(GregorianCalendar.HOUR_OF_DAY);
		int mins = dcurr.get(GregorianCalendar.MINUTE);
		int s = dcurr.get(GregorianCalendar.SECOND);
		int ms = dcurr.get(GregorianCalendar.MILLISECOND);
		double d = 2 + days + (((hours * 60 + mins) * 60 + s) * 1000 + ms)
				/ (24D * 60 * 60 * 1000) - 1;
		return d;
	}

	public static double dateTo1904(Date date) {
		if (date == null) {
			throw new NullPointerException();
		}
		GregorianCalendar d1904 = new GregorianCalendar(1904, 0, 1, 0, 0, 0);
		GregorianCalendar dcurr = new GregorianCalendar();
		dcurr.setTime(date);
		int days = getDaysBetween2Calendar(d1904, dcurr);
		int hours = dcurr.get(GregorianCalendar.HOUR_OF_DAY);
		int mins = dcurr.get(GregorianCalendar.MINUTE);
		int s = dcurr.get(GregorianCalendar.SECOND);
		int ms = dcurr.get(GregorianCalendar.MILLISECOND);
		double d = days + (((hours * 60 + mins) * 60 + s) * 1000 + ms)
				/ (24D * 60 * 60 * 1000) - 1;
		return d;
	}

	private static int getDaysBetween2Calendar(GregorianCalendar gc1,
			GregorianCalendar gc2) {
		GregorianCalendar gc1Copy = (GregorianCalendar) gc1.clone();
		GregorianCalendar gc2Copy = (GregorianCalendar) gc2.clone();
		if (gc1Copy.after(gc2Copy)) {
			GregorianCalendar temp = gc1Copy;
			gc1Copy = gc2Copy;
			gc2Copy = temp;
		}
		gc1Copy.clear(GregorianCalendar.MILLISECOND);
		gc1Copy.clear(GregorianCalendar.SECOND);
		gc1Copy.clear(GregorianCalendar.MINUTE);
		gc1Copy.clear(GregorianCalendar.HOUR_OF_DAY);

		gc2Copy.clear(GregorianCalendar.MILLISECOND);
		gc2Copy.clear(GregorianCalendar.SECOND);
		gc2Copy.clear(GregorianCalendar.MINUTE);
		gc2Copy.clear(GregorianCalendar.HOUR_OF_DAY);

		int days = 0;
		while (gc1Copy.before(gc2Copy)) {
			gc1Copy.add(GregorianCalendar.DAY_OF_MONTH, 1);
			days++;
		}
		return days;
	}
 

测试

// 1900 40448.6530580556 2010/9/27 15:40
		// 1904 38986.6523888079 2010/9/27 15:39
		System.out.println(getDaysBetween2Calendar(new GregorianCalendar(2010,
				10, 1), new GregorianCalendar(2010, 10, 2)));
		System.out.println(getDateFrom1904(38986.6523888079));
		System.out.println(getDateFrom1900(40448.6530580556));
		System.out.println(dateTo1900(getDateFrom1900(40448.6530580556)));
		System.out.println(dateTo1904(getDateFrom1904(38986.6523888079)));

 结果:

1
Mon Sep 27 15:39:26 GMT 2010
Mon Sep 27 15:40:24 GMT 2010
40448.653058055555
38986.652388807874
 
<think>好的,用户的问题是如何用Python或JavaScript将Excel文件转换为JSON格式。我需要结合提供的引用内容来构建一个清晰的回答。首先,引用1、2、3都提到了Python的方法,尤其是引用1详细介绍了使用Python开发一个小工具,引用3提供了数据源码的链接。而JavaScript方面,虽然用户提到了,但引用中没有相关信息,需要自行补充。 首先,我需要整理Python的步骤。根据引用12,Python中处理Excel常用的库是pandasopenpyxl。步骤大致是读取Excel文件,处理数据,然后转换为JSON。比如使用pandas的read_excel方法读取数据,再用to_json输出。引用2提到序列化JSON.dumps方法,这可能用于更复杂的转换,比如处理嵌套结构。 对于JavaScript部分,可能需要使用库如xlsx或exceljs。在Node.js环境中,读取Excel文件,解析数据,然后生成JSON对象,最后写入文件。这部分需要详细说明每一步的代码示例,比如使用xlsx库的readFilesheet_to_json方法。 此外,用户可能关心如何处理不同的Excel格式(xlsxlsx),是否需要处理多个工作表,或者数据类型的转换问题。引用1提到支持两种格式,所以在Python部分需要确保使用正确的引擎,比如openpyxl或xlrd。在JavaScript中,xlsx库通常支持多种格式。 还需要考虑数据结构的转换,比如是否将每个工作表转换为JSON对象中的键,或者将行数据转换为数组。引用1中的示例代码可能展示了将整个表格转换为JSON数组,每个行对应一个对象。 最后,用户可能需要注意事项,比如文件路径处理、依赖安装、错误处理等。例如,Python中需要安装pandasopenpyxl,JavaScript需要安装xlsx库。同时,处理大型文件时的性能问题,或者特殊字符的处理等。 总结回答结构:先介绍Python的实现步骤,包括安装库、代码示例说明;然后介绍JavaScript的方法,同样分步骤;最后给出注意事项总结。确保代码示例符合用户提供的引用中的方法,比如使用pandas的read_excelto_json,以及引用2中的json.dumps用于序列化。</think>### Python实现Excel转JSON的步骤 #### 1. 安装依赖库 ```bash pip install pandas openpyxl ``` #### 2. 核心代码示例 ```python import pandas as pd import json def excel_to_json(excel_path, json_path): # 读取Excel文件(支持xls/xlsx) df = pd.read_excel(excel_path, engine='openpyxl') # 转换为字典列表格式 data = df.to_dict(orient='records') # 序列化为JSON并保存 with open(json_path, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4) print(f"文件已保存至 {json_path}") # 调用示例 excel_to_json('input.xlsx', 'output.json') ``` [^1][^2] #### 3. 功能说明 - `pandas.read_excel()` 自动识别表头并转换为DataFrame - `orient='records'` 参数将数据转为`[{列1:值1,...},...]`格式 - `ensure_ascii=False` 保证中文正常显示 - `indent=4` 使JSON文件具有可读性 ### ### JavaScript实现方案(Node.js环境) #### 1. 安装依赖 ```bash npm install xlsx ``` #### 2. 核心代码示例 ```javascript const XLSX = require('xlsx'); const fs = require('fs'); function excelToJson(excelPath, jsonPath) { // 读取Excel文件 const workbook = XLSX.readFile(excelPath); // 获取第一个工作表数据 const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; // 转换为JSON const data = XLSX.utils.sheet_to_json(worksheet); // 写入文件 fs.writeFileSync(jsonPath, JSON.stringify(data, null, 2)); console.log(`文件已保存至 ${jsonPath}`); } // 调用示例 excelToJson('input.xlsx', 'output.json'); ``` ### ### 关键注意事项 1. **格式兼容性**: - Python的`openpyxl`仅支持xlsx,需`xlrd`处理xls旧格式 - JavaScript的`xlsx`库支持xls/xlsx/csv等格式 2. **数据结构处理**: - 多层表头需预处理合并单元格 - 日期字段会自动转换为JavaScript的Date对象/Python的datetime对象 3. **性能优化**: ```python # 分块读取大文件 chunk_size = 1000 for chunk in pd.read_excel('large.xlsx', chunksize=chunk_size): process(chunk) ``` ### ### 典型应用场景 1. 将产品参数表转换为API接口数据 2. 迁移Excel配置到Web应用 3. 构建自动化报表系统[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值