zoukankan
html css js c++ java
读写EXCEL的例子
private
void
process1()
{
if
(
this
.textBox_database.Text
==
""
)
{
MessageBox.Show(
"
请选择导入数据库!
"
,
"
系统信息
"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
return
;
}
if
(
this
.textBox_moban.Text
==
""
)
{
MessageBox.Show(
"
请选择模版文件!
"
,
"
系统信息
"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
return
;
}
if
(
this
.textBox_out.Text
==
""
)
{
MessageBox.Show(
"
请选择输出路径!
"
,
"
系统信息
"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
return
;
}
string
str1
=
"
InterRoll
"
;
string
str2
=
"
FileContent
"
;
this
.readdata(
this
.textBox_database.Text, str1,
"
FileRollNo
"
);
//
案卷题名数据库,以案卷号排序
//
this.readdata(this.textBox_database.Text, str2, "FileNo");
//
内容项数据库,以案卷号排序
//
遍历题名数据库
int
count1
=
this
.dataSet1.Tables[str1].Rows.Count;
this
.richTextBox1.SelectionColor
=
Color.Blue;
this
.richTextBox1.AppendText(
"
案卷分目录生成开始:\n
"
);
if
(Directory.Exists(
this
.textBox_out.Text))
{
Directory.CreateDirectory(
this
.textBox_out.Text);
}
for
(
int
i
=
0
; i
<
count1; i
++
)
{
string
[] head
=
new
string
[
this
.dataSet1.Tables[str1].Rows[i].ItemArray.Length];
//
案卷分目录题名
for
(
int
k
=
0
; k
<
this
.dataSet1.Tables[str1].Rows[i].ItemArray.Length; k
++
)
{
head[k]
=
this
.dataSet1.Tables[str1].Rows[i].ItemArray[k].ToString();
}
string
FileNo1
=
head[
1
].Trim();
string
seach
=
"
seach1
"
;
this
.searchdata(
this
.textBox_database.Text, str2,
"
FileNo = '
"
+
FileNo1
+
"
'
"
, seach);
int
count2
=
this
.dataSet1.Tables[seach].Rows.Count;
string
fileName
=
this
.textBox_moban.Text;
if
(count2
!=
0
)
{
int
finger
=
count2;
//
15行的循环计数标示
int
line
=
0
;
//
当前行
Excel.ApplicationClass excelApp
=
new
Excel.ApplicationClass();
//
Make Excel Visible
excelApp.Visible
=
false
;
//
excelApp.Visible = true;
Excel.Workbook workbook
=
excelApp.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Excel.Range range3;
int
xx
=
3
;
//
excel文件的行列
int
yy
=
6
;
object
[,] array3;
int
line_count
=
15
;
FLAG:
if
(finger
>
line_count)
{
array3
=
new
object
[
4
,
8
];
Excel._Worksheet worksheet
=
(Excel._Worksheet)workbook.Worksheets.get_Item(
1
);
range3
=
worksheet.get_Range(
"
A
"
+
xx.ToString(),
"
H
"
+
yy.ToString());
array3[
0
,
0
]
=
"
案卷题名
"
;
array3[
0
,
1
]
=
head[
2
];
//
案卷题名
array3[
0
,
4
]
=
"
档 号
"
;
array3[
0
,
5
]
=
""
;
//
档号(案卷号)
array3[
1
,
4
]
=
"
编 制 日 期
"
;
array3[
1
,
5
]
=
head[
3
];
//
编制日期
array3[
2
,
4
]
=
"
保 管 期 限
"
;
array3[
2
,
5
]
=
"
永久
"
;
//
array3[3, 4] = "共 xx 册 第 nn 册";
array3[
3
,
4
]
=
"
共
"
+
count1.ToString()
+
"
册 第
"
+
(i
+
1
).ToString()
+
"
册
"
;
range3.Value2
=
array3;
xx
+=
5
;
yy
+=
line_count
+
1
;
array3
=
new
object
[line_count,
8
];
worksheet
=
(Excel._Worksheet)workbook.Worksheets.get_Item(
1
);
range3
=
worksheet.get_Range(
"
A
"
+
xx.ToString(),
"
H
"
+
yy.ToString());
for
(
int
mm
=
0
; mm
<
line_count; mm
++
)
{
for
(
int
nn
=
0
; nn
<
8
; nn
++
)
{
array3[mm, nn]
=
""
;
}
}
for
(
int
mm
=
0
; mm
<
line_count; mm
++
)
{
/**/
/*
array3[0, 0] = "顺序号";
array3[0, 1] = "文 件 编 号";
array3[0, 2] = "责任者";
array3[0, 3] = "文 件 材 料 题 名";
array3[0, 5] = "日 期";
array3[0, 6] = "页 次";
array3[0, 7] = "备 注";
*/
array3[mm,
0
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
2
].ToString().Trim();
array3[mm,
1
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
3
].ToString().Trim();
array3[mm,
2
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
4
].ToString().Trim();
array3[mm,
3
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
5
].ToString().Trim();
array3[mm,
5
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
6
].ToString().Trim();
array3[mm,
6
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
7
].ToString().Trim();
array3[mm,
7
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
8
].ToString().Trim();
}
range3.Value2
=
array3;
line
+=
line_count;
finger
-=
line_count;
xx
+=
17
;
yy
+=
6
;
goto
FLAG;
}
else
{
array3
=
new
object
[
4
,
8
];
Excel._Worksheet worksheet
=
(Excel._Worksheet)workbook.Worksheets.get_Item(
1
);
range3
=
worksheet.get_Range(
"
A
"
+
xx.ToString(),
"
H
"
+
yy.ToString());
array3[
0
,
0
]
=
"
案卷题名
"
;
array3[
0
,
1
]
=
head[
2
];
//
案卷题名
array3[
0
,
4
]
=
"
档 号
"
;
array3[
0
,
5
]
=
""
;
//
档号(案卷号)
array3[
1
,
4
]
=
"
编 制 日 期
"
;
array3[
1
,
5
]
=
head[
3
];
//
编制日期
array3[
2
,
4
]
=
"
保 管 期 限
"
;
array3[
2
,
5
]
=
"
永久
"
;
//
array3[3, 4] = "共 xx 册 第 nn 册";
array3[
3
,
4
]
=
"
共
"
+
count1.ToString()
+
"
册 第
"
+
(i
+
1
).ToString()
+
"
册
"
;
range3.Value2
=
array3;
xx
+=
5
;
yy
+=
line_count;
array3
=
new
object
[line_count,
8
];
worksheet
=
(Excel._Worksheet)workbook.Worksheets.get_Item(
1
);
range3
=
worksheet.get_Range(
"
A
"
+
xx.ToString(),
"
H
"
+
yy.ToString());
for
(
int
mm
=
0
; mm
<
15
; mm
++
)
{
for
(
int
nn
=
0
; nn
<
8
; nn
++
)
{
array3[mm, nn]
=
""
;
}
}
for
(
int
mm
=
0
; mm
<
finger; mm
++
)
{
array3[mm,
0
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
2
].ToString().Trim();
array3[mm,
1
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
3
].ToString().Trim();
array3[mm,
2
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
4
].ToString().Trim();
array3[mm,
3
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
5
].ToString().Trim();
array3[mm,
5
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
6
].ToString().Trim();
array3[mm,
6
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
7
].ToString().Trim();
array3[mm,
7
]
=
this
.dataSet1.Tables[seach].Rows[line
+
mm].ItemArray[
8
].ToString().Trim();
}
range3.Value2
=
array3;
line
+=
line_count;
finger
=
0
;
xx
+=
17
;
//
表格指向最后一行
yy
+=
2
;
}
//
删除多余的表格,
Excel._Worksheet worksheet2
=
(Excel._Worksheet)workbook.Worksheets.get_Item(
1
);
range3
=
worksheet2.get_Range(
"
A
"
+
xx.ToString(),
"
H484
"
);
//
range3.Clear();
range3.Delete(Missing.Value);
//
excel 2000/xp
string
savename
=
this
.textBox_out.Text
+
"
\\
"
+
FileNo1
+
"
.xls
"
;
if
(File.Exists(savename))
{
File.Delete(savename);
}
workbook._SaveAs(savename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
workbook.Saved
=
true
;
workbook.Close(Missing.Value, Missing.Value, Missing.Value);
excelApp.UserControl
=
false
;
excelApp.Quit();
this
.richTextBox1.SelectionColor
=
Color.Green;
this
.richTextBox1.AppendText(
"
案卷号:
"
+
FileNo1
+
"
添加成功!\n
"
);
}
else
{
this
.richTextBox1.SelectionColor
=
Color.Red;
this
.richTextBox1.AppendText(
"
案卷号:
"
+
FileNo1
+
"
对应的内容项不存在!\n
"
);
}
}
this
.richTextBox1.SelectionColor
=
Color.Blue;
this
.richTextBox1.AppendText(
"
案卷分目录生成成功!\n
"
);
}
查看全文
相关阅读:
Ubuntu20.04 安装AMD显卡驱动
Ubuntu IBus RIME 输入法 配置小鹤双拼
python time常用转换
Linux 目录软链接
MarkDown 插入图片 && Picture To Base64
Bitmap 简介
Linux 使用命令发送邮件
Linux分区
PyQt graphicsView自适应显示图像
Python 图片转视频
原文地址:https://www.cnblogs.com/dreign/p/576090.html
最新文章
Nacos集群部署
CentOS7和Ubuntu18.04不同系统如何搭建mysql服务
Linux系统安装jdk环境
idea远程调试Springboot项目
全文检索Elasticsearch研究
CentOS7扩展磁盘空间
Spring Cloud Ribbon
SpringCloud简介初体验
Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
CentOS7最小化安装后要做的事
热门文章
评估磁盘读写性能极限
top命令
Linux查看系统中的服务、端口等信息
sar命令之Network网络监控(四)
sar命令之IO监控(三)
sar命令之内存监控(二)
sysstat中sar命令之CPU监控(一)
Linux查看服务器相关信息
linux操作防火墙,添加服务、端口
Linux查看程序是否退出,退出重启
Copyright © 2011-2022 走看看