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
"
);
}
查看全文
相关阅读:
皇帝的用人之道,这一点古今皆同
sharepoint打包
powershellbegin
taxonomy
powershelluninstall webapplication
面试题
字符串处理
在页面中插入视频时的文件夹命名问题
process object
扩展名显示与隐藏
原文地址:https://www.cnblogs.com/dreign/p/576090.html
最新文章
MFC框架中对最近文件列表的支持
白领饮食十大“夺命恶习”
分享]人生忠告——七天改变人生影响世界
游成都天府广场有感
2007新年个人时间管理解析
劳累一天后什么样的睡姿最健康?
分享]9大新职业2007年将亮相
人生创业的四个象限
个人的发展遭受阻碍,职场白领之“伤城”
转帖]送给要跳槽人们的深刻寓言
热门文章
07冲杀职场必知的4只魔戒N条箴言
上班族不知不觉发财的十大秘诀
给IT从业人员的一些建议
一个秘书MM的搞笑日记,笑掉大牙(转)
行走职场,要以优雅的姿态离职!!!
创业者的关键词:坚持、忍耐、积累
一个人的情人节
四步教你打开职场知名度
无论结婚与否,职场女性的桃色陷阱分类(转)
2007年跳槽牛人的十四条钻石真经
Copyright © 2011-2022 走看看