当前位置:网站首页>针对大量数据,MATLAB生成EXCEL文件并进行排版处理的源码
针对大量数据,MATLAB生成EXCEL文件并进行排版处理的源码
2022-07-28 05:23:00 【沱江一苇】
#MATLAB生成EXCEL文件并进行排版处理的源码
项目要求:给定某海洋站潮位数据在一年之年的每分钟潮位数据(一共有24×60×365=525600个数),txt格式,要求制作出海洋站该年份的潮汐表
实现方案:MATLAB对该txt文件进行处理,生成excel工作簿,每一张工作表为潮位
一 天之内的数据,一共365张工作表。每张表即24*60=1440个数据,横坐标为分钟,纵坐标为小时,还要包括有经纬度、日期、所在时区等信息
程序实现功能:
1、matlab生成excel表格 actxserver(‘excel.application’)
2、matlab设置excel单元格的列高、行宽、横向对齐方式、垂直对齐方式、单元格字体大小、是否加粗等操作,见程序 单元格参数设置部分
3、matlab实现对excel表格的合并单元格、统一单元格的数值显示位数(0000格式) 等操作 见程序,NumberFormatLocal=‘0000’
4、实现当前天数到日期转换 例如第35天→2019/02/04,见程序,datestr()
5、数字到字符的转变 见程序,num2str()、cellstr()
[email protected]
%%用A4纸张打印出来
clc
clear
A=load ('某某港原始数据.txt'); %调整下潮汐表每分钟数据格式
A_1=A(:,5:64);
A_2=zeros(17520,30);
for i=1:8670
A_2(2*i-1,:)=A_1(i,1:30);
A_2(2*i,:)=A_1(i,31:60);
end
AT=load ('某某港高低潮数据.txt');
AT_1=AT(:,26:33);
chaote=cell(365,8);
for jj=1:365
for kk=1:4
chaote{jj,2*kk-1}=num2str(AT_1(jj,2*kk-1),'%04d');
chaote{jj,2*kk}=num2str(AT_1(jj,2*kk));
end
end
zongriqi=365;
%表中与表头部分,写入表的实际值和表头的横坐标
fenzhong=cell(1,30);
for j=0:29
fen1=num2str(j);fen2=num2str((j+30));
fen=strcat(fen1,'\',fen2);
fenzhong{1,j+1}=fen;
end
for i=1:zongriqi %每一天做为一页,写入表中实际数据和表横坐标“分钟”
B=A_2(48*(i-1)+1:48*i,1:30);
data=num2str(i);
Sheet=strcat('Sheet',data);
xlswrite('潮汐表.xlsx',B,Sheet,'C10:AF57'); %写入实际数据
xlswrite('潮汐表.xlsx',fenzhong,Sheet,'C9:AF9'); %写入表的横坐标
end
file_path=[pwd,'\潮汐表.xlsx']; %设置当前路径
hExcel=actxserver('excel.application'); %创建一个EXCEL服务器,并返回句柄
set(hExcel,'Visible',1); %设置Excel服务器为可见状态
hWorkbooks=hExcel.Workbooks.Open(file_path); %打开文件
hSheets=hExcel.ActiveWorkBook.Sheets; %返回当前工作表句柄
for k=1:zongriqi %每一天做为一页,写入表中数据
hSheetk=hSheets.Item(k); %返回第k个表格句柄
hSheetk.Activate; %激活该表格
hSheetk.Range('C1:AF58').ColumnWidth=4; %设置Range对象的列宽
hSheetk.Range('C1:AF58').RowHeight=25; %设置Range对象的行高
hSheetk.Range('C1:AF58').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('C1:AF58').Font.size=14; %设置单元格字体大小
hSheetk.Range('C1:AF58').HorizontalAlignment=3; %设置单元格对齐方式横向居中
hSheetk.Range('C1:AF58').VerticalAlignment=2; %设置单元格对齐方式竖直方向居中
%hSheetk.Range('C1:AF49').Font.bold=2; %设置单元格字体格式为加粗
hSheetk.Range('C9:AF9').Font.size=9; %设置单元格字体大小
%表头部分,包括“某某港(中文、英语,居中)”
hSheetk.Range('A1:AF2').MergeCells=1; %合并单元格
hSheetk.Range('A1:AF2').Value='某某港'; %写入单元格内容
hSheetk.Range('A3:AF3').MergeCells=1; %合并单元格
hSheetk.Range('A3:AF3').Value='XX_PORT'; %写入单元格内容
hSheetk.Range('A1:AF2').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('A1:AF2').Font.size=36; %设置单元格字体大小
hSheetk.Range('A3:AF3').Font.size=18; %设置单元格字体大小
hSheetk.Range('A1:AF3').HorizontalAlignment=3; %设置单元格对齐方式,居中
hSheetk.Range('A1:AF3').VerticalAlignment=2; %设置单元格对齐方式,居中
%表头部分,包括“2019年1月潮汐表 经纬度xx′S,xxE” 1日
hSheetk.Range('A4:D4').MergeCells=1; %合并单元格
Num=datenum('1-January-2019');
num=Num+k-1;
date_str=datestr(num,'yyyy/mm/dd');
hSheetk.Range('A4:D4').MergeCells=1; %合并单元格
hSheetk.Range('A4:D4').Value=date_str; %写入当前日期
hSheetk.Range('M4:R4').MergeCells=1; %合并单元格
hSheetk.Range('M4:R4').Value=xxS,xx′E'; %写入当前经纬度
hSheetk.Range('A4:D4').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('A4:D4').Font.size=22; %设置单元格字体大小
hSheetk.Range('A4:D4').HorizontalAlignment=3; %设置单元格对齐方式,居中
hSheetk.Range('A4:D4').VerticalAlignment=2; %设置单元格对齐方式,居中
%表头部分,写入表的纵坐标,小时
for s=1:24
shi1=num2str((2*s+8));shi2=num2str((2*s+9));shi3=num2str((s-1));
shi=strcat('B',shi1,':','B',shi2);
hSheetk.Range(shi).MergeCells=1; %合并单元格
hSheetk.Range(shi).Value=shi3; %写入单元格内容
end
hSheetk.Range('B10:B57').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('B10:B57').Font.size=22; %设置单元格字体大小
hSheetk.Range('B10:B57').Font.bold=2; %设置单元格字体格式为加粗
hSheetk.Range('B10:B57').HorizontalAlignment=3; %设置单元格对齐方式
hSheetk.Range('B10:B57').VerticalAlignment=2; %设置单元格对齐方式
%表头部分,写入表的横纵坐标意义,小时\分钟
hSheetk.Range('B8:B9').MergeCells=1; %合并单元格
hSheetk.Range('B8:B9').Value='H\M'; %写入单元格内容
hSheetk.Range('B8:B9').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('B8:B9').Font.size=16; %设置单元格字体大小
hSheetk.Range('B8:B9').Font.bold=2; %设置单元格字体格式为加粗
hSheetk.Range('B8:B9').HorizontalAlignment=3; %设置单元格对齐方式
hSheetk.Range('B8:B9').VerticalAlignment=2; %设置单元格对齐方式
%表头部分,写入表的横纵坐标含义,上排0:29各分钟,下排30:59各分钟
hSheetk.Range('C8:AF8').Value=0:1:29; %写入单元格内容
hSheetk.Range('C9:AF9').Value=30:1:59; %写入单元格内容
hSheetk.Range('C8:AF9').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('C8:AF9').Font.size=14; %设置单元格字体大小
hSheetk.Range('C8:AF9').Font.bold=2; %设置单元格字体格式为加粗
hSheetk.Range('C8:AF9').HorizontalAlignment=3; %设置单元格对齐方式
hSheetk.Range('C8:AF9').VerticalAlignment=2; %设置单元格对齐方式
%表头表尾部分,将表的最顶端的上边框与最底端的下边框加粗
hSheetk.Range('B8:AF8').Borders.Item(3).Weight=4; %设定表格的上边框为线段加粗
hSheetk.Range('B57:AF57').Borders.Item(4).Weight=4; %设定表格的下边框为线段加粗
%表尾部分,写入时区与潮高基准面
hSheetk.Range('B58:G58').MergeCells=1; %合并单元格
hSheetk.Range('B58:G58').Value='Time Zone: -1100'; %写入单元格内容
hSheetk.Range('S58:AF58').MergeCells=1; %合并单元格
hSheetk.Range('S58:AF58').Value='Tidal high datum: 83cm below average sea level'; %写入单元格内容
hSheetk.Range('B58:AF58').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('B58:AF58').Font.size=14; %设置单元格字体大小
hSheetk.Range('B58:AF58').Font.bold=2; %设置单元格字体格式为加粗
hSheetk.Range('B58:AF58').RowHeight=35; %设置单元格的行高
hSheetk.Range('B58:AF58').HorizontalAlignment=3; %设置单元格对齐方式
hSheetk.Range('B58:AF58').VerticalAlignment=2; %设置单元格对齐方式
%表头部分,写入潮汐表每天的最高潮与对应潮时
hSheetk.Range('D6:H6').MergeCells=1; %合并单元格
hSheetk.Range('D6:H6').Value='Tide time(h:m)'; %写入单元格内容,潮时表头
hSheetk.Range('D7:H7').MergeCells=1; %合并单元格
hSheetk.Range('D7:H7').Value='Tide height(cm)'; %写入单元格内容,潮高表头
hSheetk.Range('D6:H7').HorizontalAlignment=2; %设置单元格对齐方式为左对齐
hSheetk.Range('J6:AF7').HorizontalAlignment=4; %设置单元格对齐方式为右对齐
%将单元格合并,设定为右对齐,统一为4位数,并写入单元格内容,4个潮高对应的时间,
hSheetk.Range('J6:K6').MergeCells=1;hSheetk.Range('M6:N6').MergeCells=1;hSheetk.Range('P6:Q6').MergeCells=1; hSheetk.Range('S6:T6').MergeCells=1;
hSheetk.Range('J6:K6').NumberFormatLocal='0000';hSheetk.Range('M6:N6').NumberFormatLocal='0000';%格式统一为4位数
hSheetk.Range('P6:Q6').NumberFormatLocal='0000';hSheetk.Range('S6:T6').NumberFormatLocal='0000';%格式统一为4位数
hSheetk.Range('J6:K6').Value=cellstr(chaote{k,1});hSheetk.Range('M6:N6').Value=num2str(chaote{k,3},'%04d'); %写入潮时
hSheetk.Range('P6:Q6').Value=num2str(chaote{k,5},'%04d');hSheetk.Range('S6:T6').Value=num2str(chaote{k,7},'%04d'); %写入潮时
%将单元格合并,设定为右对齐,并写入单元格内容,4个潮高
hSheetk.Range('J7:K7').MergeCells=1;hSheetk.Range('M7:N7').MergeCells=1;hSheetk.Range('P7:Q7').MergeCells=1; hSheetk.Range('S7:T7').MergeCells=1;
hSheetk.Range('J7:K7').Value=num2str(chaote{k,2});hSheetk.Range('M7:N7').Value=num2str(chaote{k,4});%写入潮高
hSheetk.Range('P7:Q7').Value=num2str(chaote{k,6});hSheetk.Range('S7:T7').Value=num2str(chaote{k,8});%写入潮高
hSheetk.Range('D6:AF7').Font.name='宋体'; %设置单元格字体大小
hSheetk.Range('D6:H7').Font.size=14; %设置单元格字体大小,表头大小
hSheetk.Range('D6:H7').Font.bold=2; %设置单元格字体格式为加粗,对表头进行加粗
hSheetk.Range('J6:AF7').Font.size=20; %设置单元格字体大小,潮高和潮时大小
end
Quit(hExcel);
delete(hExcel);
在这里插入代码片
边栏推荐
- Differences between processes and threads
- TensorFlow2.1基本概念与常见函数
- USB Network Native Driver for ESXi更新到支持ESXi7.0 Update 2
- Which is more reliable for small program development?
- 关于Fusion on Apple Silicon的谨慎猜测
- How to choose an applet development enterprise
- 深度学习(增量学习)——(ICCV)Striking a Balance between Stability and Plasticity for Class-Incremental Learning
- Byte Android post 4 rounds of interviews, received 50k*18 offers, and successfully broke the situation under the layoff
- Four perspectives to teach you to choose applet development tools?
- Convolutional neural network
猜你喜欢

关于Fusion on Apple Silicon的谨慎猜测

Getting started with latex

Convolutional neural network

深度学习(自监督:MoCo v2)——Improved Baselines with Momentum Contrastive Learning

Deep learning (II) into machine learning and deep learning programming

D2sc-gan: low resolution face recognition of classroom scenes based on dual depth and shallow channel generation confrontation network

一、AMD - OpenVINO环境配置

Deep learning (self supervision: simple Siam) -- Exploring simple Siamese representation learning

Geek challenge 2019-sql injection five questions PW

Reinforcement learning -- SARS in value learning
随机推荐
D2sc-gan: low resolution face recognition of classroom scenes based on dual depth and shallow channel generation confrontation network
2、 Openvino brief introduction and construction process
生活随机-1
EIGamal cryptosystem description
基于tensorflow搭建神经网络
Reinforcement learning - proximal policy optimization algorithms
What are the detailed steps of wechat applet development?
压敏电阻设计参数及经典电路记录 硬件学习笔记5
关于隔离电源断电瞬间MOSFET损坏问题分析
ESXi 社区版网卡驱动
Differences between processes and threads
Getting started with latex
ESXi社区版网卡驱动再次更新
Protecting Against DNN Model Stealing Attacks 论文阅读心得
神经网络优化
Model Inversion Attacks that Exploit Confidence Informati on and Basic Countermeasures 阅读心得
循环神经网络
Deep learning (self supervision: Moco V2) -- improved bases with momentum contractual learning
后门攻击与对抗样本攻击的比较研究
Building neural network based on tensorflow