读写EXCEL的例子

None.gif    private   void  process1()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (this.textBox_database.Text == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"请选择导入数据库!""系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this.textBox_moban.Text == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"请选择模版文件!""系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this.textBox_out.Text == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"请选择输出路径!""系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string str1 = "InterRoll";
InBlock.gif            
string str2 = "FileContent";
InBlock.gif            
this.readdata(this.textBox_database.Text, str1, "FileRollNo");//案卷题名数据库,以案卷号排序
InBlock.gif            
//this.readdata(this.textBox_database.Text, str2, "FileNo");//内容项数据库,以案卷号排序
InBlock.gif            
//遍历题名数据库
InBlock.gif
            int count1 = this.dataSet1.Tables[str1].Rows.Count;
InBlock.gif            
this.richTextBox1.SelectionColor = Color.Blue;
InBlock.gif            
this.richTextBox1.AppendText("案卷分目录生成开始:\n");
InBlock.gif
InBlock.gif            
if (Directory.Exists(this.textBox_out.Text))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Directory.CreateDirectory(
this.textBox_out.Text);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
for (int i = 0; i < count1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string[] head = new string[this.dataSet1.Tables[str1].Rows[i].ItemArray.Length];//案卷分目录题名
InBlock.gif
                for (int k = 0; k < this.dataSet1.Tables[str1].Rows[i].ItemArray.Length; k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    head[k] 
= this.dataSet1.Tables[str1].Rows[i].ItemArray[k].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
string FileNo1 = head[1].Trim();
InBlock.gif
InBlock.gif                
string seach = "seach1";
InBlock.gif                
this.searchdata(this.textBox_database.Text, str2, " FileNo = '" + FileNo1 + "'", seach);
InBlock.gif
InBlock.gif                
int count2 = this.dataSet1.Tables[seach].Rows.Count;
InBlock.gif
InBlock.gif                
string fileName = this.textBox_moban.Text;
InBlock.gif                
if (count2 != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int finger = count2;//15行的循环计数标示
InBlock.gif
                    int line = 0;       //当前行
InBlock.gif
                    Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
InBlock.gif
InBlock.gif                    
//Make Excel Visible
InBlock.gif
                    excelApp.Visible = false;
InBlock.gif                    
// excelApp.Visible = true;
InBlock.gif
                    Excel.Workbook workbook =
InBlock.gif                        excelApp.Workbooks.Open(fileName,
InBlock.gif                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
InBlock.gif                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
InBlock.gif                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
InBlock.gif                        Type.Missing, Type.Missing);
InBlock.gif
InBlock.gif                    Excel.Range range3;
InBlock.gif
InBlock.gif                    
int xx = 3;//excel文件的行列
InBlock.gif
                    int yy = 6;
InBlock.gif                    
object[,] array3;
InBlock.gif                    
int line_count = 15;
InBlock.gif                FLAG:
InBlock.gif                    
if (finger > line_count)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        array3 
= new object[48];
InBlock.gif                        Excel._Worksheet worksheet 
= (Excel._Worksheet)workbook.Worksheets.get_Item(1);
InBlock.gif                        range3 
= worksheet.get_Range("A" + xx.ToString(), "H" + yy.ToString());
InBlock.gif                        array3[
00= "案卷题名";
InBlock.gif                        array3[
01= head[2];//案卷题名
InBlock.gif
                        array3[04= "档           号";
InBlock.gif                        array3[
05= "";//档号(案卷号)
InBlock.gif
                        array3[14= "编 制 日 期";
InBlock.gif                        array3[
15= head[3];//编制日期
InBlock.gif
                        array3[24= "保 管 期 限";
InBlock.gif                        array3[
25= "永久";
InBlock.gif                        
//array3[3, 4] = "共  xx  册    第  nn  册";
InBlock.gif
                        array3[34= "共  " + count1.ToString() + "  册    第  " + (i + 1).ToString() + "  册";
InBlock.gif                        range3.Value2 
= array3;
InBlock.gif
InBlock.gif                        xx 
+= 5;
InBlock.gif                        yy 
+= line_count + 1;
InBlock.gif                        array3 
= new object[line_count, 8];
InBlock.gif                        worksheet 
= (Excel._Worksheet)workbook.Worksheets.get_Item(1);
InBlock.gif                        range3 
= worksheet.get_Range("A" + xx.ToString(), "H" + yy.ToString());
InBlock.gif
InBlock.gif                        
for (int mm = 0; mm < line_count; mm++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
for (int nn = 0; nn < 8; nn++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                array3[mm, nn] 
= "";
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
for (int mm = 0; mm < line_count; mm++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
/**//*
InBlock.gif                      array3[0, 0] = "顺序号";
InBlock.gif                      array3[0, 1] = "文 件  编 号";
InBlock.gif                      array3[0, 2] = "责任者";
InBlock.gif                      array3[0, 3] = "文    件    材    料    题    名";
InBlock.gif                      array3[0, 5] = "日    期";
InBlock.gif                      array3[0, 6] = "页      次";
InBlock.gif                      array3[0, 7] = "备      注";
ExpandedSubBlockEnd.gif                     
*/

InBlock.gif
InBlock.gif                            array3[mm, 
0= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[2].ToString().Trim();
InBlock.gif                            array3[mm, 
1= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[3].ToString().Trim();
InBlock.gif                            array3[mm, 
2= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[4].ToString().Trim();
InBlock.gif                            array3[mm, 
3= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[5].ToString().Trim();
InBlock.gif                            array3[mm, 
5= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[6].ToString().Trim();
InBlock.gif                            array3[mm, 
6= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[7].ToString().Trim();
InBlock.gif                            array3[mm, 
7= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[8].ToString().Trim();
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        range3.Value2 
= array3;
InBlock.gif                        line 
+= line_count;
InBlock.gif                        finger 
-= line_count;
InBlock.gif                        xx 
+= 17;
InBlock.gif                        yy 
+= 6;
InBlock.gif                        
goto FLAG;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        array3 
= new object[48];
InBlock.gif                        Excel._Worksheet worksheet 
= (Excel._Worksheet)workbook.Worksheets.get_Item(1);
InBlock.gif                        range3 
= worksheet.get_Range("A" + xx.ToString(), "H" + yy.ToString());
InBlock.gif                        array3[
00= "案卷题名";
InBlock.gif                        array3[
01= head[2];//案卷题名
InBlock.gif
                        array3[04= "档           号";
InBlock.gif                        array3[
05= "";//档号(案卷号)
InBlock.gif
                        array3[14= "编 制 日 期";
InBlock.gif                        array3[
15= head[3];//编制日期
InBlock.gif
                        array3[24= "保 管 期 限";
InBlock.gif                        array3[
25= "永久";
InBlock.gif                        
//array3[3, 4] = "共  xx  册    第  nn  册";
InBlock.gif
                        array3[34= "共  " + count1.ToString() + "  册    第  " + (i + 1).ToString() + "  册";
InBlock.gif                        range3.Value2 
= array3;
InBlock.gif
InBlock.gif                        xx 
+= 5;
InBlock.gif                        yy 
+= line_count;
InBlock.gif                        array3 
= new object[line_count, 8];
InBlock.gif                        worksheet 
= (Excel._Worksheet)workbook.Worksheets.get_Item(1);
InBlock.gif                        range3 
= worksheet.get_Range("A" + xx.ToString(), "H" + yy.ToString());
InBlock.gif
InBlock.gif                        
for (int mm = 0; mm < 15; mm++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
for (int nn = 0; nn < 8; nn++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                array3[mm, nn] 
= "";
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
for (int mm = 0; mm < finger; mm++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            array3[mm, 
0= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[2].ToString().Trim();
InBlock.gif                            array3[mm, 
1= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[3].ToString().Trim();
InBlock.gif                            array3[mm, 
2= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[4].ToString().Trim();
InBlock.gif                            array3[mm, 
3= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[5].ToString().Trim();
InBlock.gif                            array3[mm, 
5= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[6].ToString().Trim();
InBlock.gif                            array3[mm, 
6= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[7].ToString().Trim();
InBlock.gif                            array3[mm, 
7= this.dataSet1.Tables[seach].Rows[line + mm].ItemArray[8].ToString().Trim();
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        range3.Value2 
= array3;
InBlock.gif                        line 
+= line_count;
InBlock.gif                        finger 
= 0;
InBlock.gif
InBlock.gif                        xx 
+= 17;//表格指向最后一行
InBlock.gif
                        yy += 2;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
//删除多余的表格,
InBlock.gif
                    Excel._Worksheet worksheet2 = (Excel._Worksheet)workbook.Worksheets.get_Item(1);
InBlock.gif                    range3 
= worksheet2.get_Range("A" + xx.ToString(), "H484");
InBlock.gif                    
//range3.Clear();
InBlock.gif
                    range3.Delete(Missing.Value);
InBlock.gif                    
//excel 2000/xp        
InBlock.gif
                    string savename = this.textBox_out.Text + "\\" + FileNo1 + ".xls";
InBlock.gif                    
if (File.Exists(savename))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        File.Delete(savename);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    workbook._SaveAs(savename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
InBlock.gif                       Excel.XlSaveAsAccessMode.xlNoChange,
InBlock.gif                       Missing.Value, Missing.Value, Missing.Value, Missing.Value);
InBlock.gif
InBlock.gif                    workbook.Saved 
= true;
InBlock.gif                    workbook.Close(Missing.Value, Missing.Value, Missing.Value);
InBlock.gif
InBlock.gif                    excelApp.UserControl 
= false;
InBlock.gif                    excelApp.Quit();
InBlock.gif
InBlock.gif                    
this.richTextBox1.SelectionColor = Color.Green;
InBlock.gif                    
this.richTextBox1.AppendText("案卷号:" + FileNo1 + " 添加成功!\n");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.richTextBox1.SelectionColor = Color.Red;
InBlock.gif                    
this.richTextBox1.AppendText("案卷号:" + FileNo1 + " 对应的内容项不存在!\n");
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.richTextBox1.SelectionColor = Color.Blue;
InBlock.gif            
this.richTextBox1.AppendText("案卷分目录生成成功!\n");
ExpandedBlockEnd.gif        }

None.gif      

转载于:https://www.cnblogs.com/dreign/archive/2006/11/29/576090.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值