读写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[48];
                        Excel._Worksheet worksheet 
= (Excel._Worksheet)workbook.Worksheets.get_Item(1);
                        range3 
= worksheet.get_Range("A" + xx.ToString(), "H" + yy.ToString());
                        array3[
00= "案卷题名";
                        array3[
01= head[2];//案卷题名
                        array3[04= "档           号";
                        array3[
05= "";//档号(案卷号)
                        array3[14= "编 制 日 期";
                        array3[
15= head[3];//编制日期
                        array3[24= "保 管 期 限";
                        array3[
25= "永久";
                        
//array3[3, 4] = "共  xx  册    第  nn  册";
                        array3[34= "共  " + 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[48];
                        Excel._Worksheet worksheet 
= (Excel._Worksheet)workbook.Worksheets.get_Item(1);
                        range3 
= worksheet.get_Range("A" + xx.ToString(), "H" + yy.ToString());
                        array3[
00= "案卷题名";
                        array3[
01= head[2];//案卷题名
                        array3[04= "档           号";
                        array3[
05= "";//档号(案卷号)
                        array3[14= "编 制 日 期";
                        array3[
15= head[3];//编制日期
                        array3[24= "保 管 期 限";
                        array3[
25= "永久";
                        
//array3[3, 4] = "共  xx  册    第  nn  册";
                        array3[34= "共  " + 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");
        }

      
原文地址:https://www.cnblogs.com/dreign/p/576090.html