简体   繁体   English

如何基于特定列拆分excel文件

[英]How to split excel file based on Specific Column

I have example data like this (in Excel) 我有像这样的示例数据(在Excel中)

Name Place A JKT B JKT C CGK D JKT E CGK F BBK G JKT H BBK I BBK J BBK K CGK L CGK 名称放置A JKT B JKT C CGK D JKT E CGK F BBK G JKT H BBK I BBK J BBK K CGK L CGK

I've been given 3++ files that contain data like the example, and the step is , i must combine the files first into 1 excel file, than the output that i already combine must be split into many excel file based on Place. 我已经获得了包含数据的3个++文件,例如,步骤是,我必须首先将文件合并到1个excel文件中,而我已经组合的输出必须根据Place拆分成许多excel文件。

Ex : in that data has 3 different places, so it will create 3 different files for JKT,CGK and BBK 例如:因为数据有3个不同的位置,所以它将为JKT,CGK和BBK创建3个不同的文件

The code i tried to combine 我试图结合的代码

xlApp = new excel.Application();
 xlWb = xlApp.Workbooks.Open(fileName);
 xlWs = xlWb.Sheets[1];
 xlRange = xlWs.UsedRange;

 row = xlRange.Rows.Count;
 col = xlRange.Columns.Count;
 fulldata = new string[row][];

 for (int i = 0; i < fulldata.Length; i++)
 {
    fulldata[i] = new string[col];
    //MessageBox.Show(i.ToString());
 }

 for (int i = 0; i < row; i++)
 {
     for (int j = 0; j < col; j++)
     {
         try
         {
            fulldata[i][j] = xlWs.Cells[i + 1, j + 1].value2.ToString();
         }
         catch (Exception ee)
         {
             fulldata[i][j] = "";
         }
      }
 }

//create new excel file for combined data

xlApp1 = new excel.Application();
xlWb1 = xlApp1.Workbooks.Add();
xlWs1 = (excel.Worksheet)xlWb1.Worksheets.get_Item(1);
xlRange1 = xlWs1.UsedRange;

newFileRow = xlRange1.Rows.Count;
newFileCol = xlRange1.Columns.Count;

//Combine
if (newFileRow == 1)
{
   for (int i = 0; i < row; i++)
   {
       for (int j = 0; j < col; j++)
       {
           xlWs1.Cells[i + 1, j + 1] = fulldata[i][j];
       }
   }
}
else
{
   for (int i = newFileRow+1; i < row + newFileRow; i++)
   {
       for (int j = 0; j < col; j++)
       {
           xlWs1.Cells[i, j + 1] = fulldata[count][j];
           count++;
        }
     }
}

//Split ExcelFile
for (int i = 0; i < row; i++)
   {
       for (int j = 0; j < col; j++)
       {
           //insertcode here
       }
   }

The problem is , the data has more than 40 Place , so i dont know the effective way to split it, because if i use the hardcode mode it will be not efficiency and hard to read. 问题是,数据有超过40个地方,所以我不知道分割它的有效方法,因为如果我使用硬编码模式,它将不是效率和难以阅读。

Hardcode = defines variable for 40 different places, define excel.Application for 40 different excels, and use if for 40 ++ different places Hardcode =为40个不同的地方定义变量,定义40个不同excel的excel.Application,并使用if为40 ++个不同的地方

Expected result (from example): 1st excel : JKT.xlsx 预期结果(来自示例):第一个excel:JKT.xlsx

Name       Place
A          JKT
B          JKT
D          JKT

2nd excel : CGK.xlsx 第二名:CGK.xlsx

Name       Place
C          CGK
E          CGK
K          CGK 

3rd excel : BBK.xlsx 第3名excel:BBK.xlsx

Name       Place
F          BBK

Any help will be appreciated and grammar correction 任何帮助将不胜感激和语法修正

Why dont you use query? 为什么不使用查询? After you read the file, put the ORDER BY Statement. 读取文件后,输入ORDER BY语句。

SqlCommand cmd = "SELECT NAME, PLACE FROM namaTabel ORDER BY PLACE ASC"; 
cmd.fulldata();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM