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
"
);
}
查看全文
相关阅读:
《魏坤琳的科学育儿宝典》笔记
大千世界中,万物皆有时节
心里健康基本要素
不带评论的观察是人类智力的最高形式
《七律·人民解放军占领南京》
《最好的防御就是进攻》-- 向任总汇报无线业务会议纪要 2013年9月5日
《深淘滩,低作堰》-- 任正非在运作与交付体系奋斗表彰大会上的讲话 2009年4月24日
《市场经济是最好的竞争方式,经济全球化是不可阻挡的潮流》-- 任正非在财经系统表彰大会上的讲话 2009年3月24日
《开放、妥协与灰度》-- 任正非在2009年全球市场工作会议上的讲话 2009年1月15日
《让青春的火花,点燃无愧无悔的人生》-- 任正非在无线产品线奋斗大会上的讲话纪要 2008年5月31日
原文地址:https://www.cnblogs.com/dreign/p/576090.html
最新文章
【Python基础】IndentationError: unexpected indent
【Python基础】简单的数组例子
【Python基础】我的第一个 python 程序
Shell笔记
k8s笔记
docker安装elasticsearch
Winlogbeat配置负责均衡
Docker安装logstash
Docker安装Kibana
Docker网络配置
热门文章
Docker volume 数据持久化保存
docker-compose.yml编写
Dockerfile多阶段构建之from==0
python3 爬虫Chromedriver和Chrome版本不匹配问题
ffmpeg视频抽取帧
《管理:任务、责任和实践》笔记
《品牌传播》笔记
《基业长青》笔记
《从优秀到卓越》笔记
《为何家会伤人》笔记
Copyright © 2011-2022 走看看