读取Excel文件并生成包含其他信息的新Excel文件 [英] Reading an Excel file and Generating a new excel file with additional information

查看:95
本文介绍了读取Excel文件并生成包含其他信息的新Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在编写一个创建excel文件的应用程序。 excel的内容将是另一个将被读入的excel的一部分。我只需要例如,读取excel并从第2行到第3行复制所有内容,并将其粘贴到新的excel中。

我可以生成并且没有问题,但是很难读出excel。





任何想法?

Hi
im writing an app that creates an excel file. The contents of the excel will be parts of another excel that will be read in. i just need to for example, read the excel and copy everything from row 2 to 3, and paste it in the new excel.
I can generate and excel with no problem, but finding it difficult to read an excel.


any ideas?

推荐答案

//你必须使用GemBox.Spreadsheet下载GemBox.Spreadsheet.dll

;





ExcelFile ef = null;

ef.LoadXls(_filePath);

// for get文本

string [] array = new string [ef.Worksheets.Count];

int i = 0;

foreach(ExcelWorksheet ws in ef.Worksheets)

{

array [i] = ws.Name;

i ++;

} < br $>


string text =;



for(int j = 0; j< i; j ++ )

{

ExcelWorksheet sheet = ef.Worksheets [array [j]];



foreach(ExcelRow er in sheet.Rows)

{

foreach(ExcelCell ec in er.Cells)

{

if(ec.Value!= null)

text + = ec.Value.ToString()+;



}

text + =\ n;

}

text + =\ n \ n ;

}

//获取Rich Text

string text =;



for(int j = 0; j<一世; j ++)

{

ExcelWorksheet sheet = ef.Worksheets [array [j]];



foreach( ExcelRow er in sheet.Rows)

{

foreach(ExcelCell ec in er.Cells)

{

if(ec.Value!= null)

{

text + = ec.Value.ToString()+\ n(;

text + = ec.Style.Font.Name +,+ ec.Style.Font.Color.ToString()+)\ n;

}



}

text + =\ n\ n;

}

text + =\ n \ nn \ n;

}
// you have to download GemBox.Spreadsheet.dll
using GemBox.Spreadsheet;


ExcelFile ef = null;
ef.LoadXls(_filePath);
//for get text
string[] array = new string[ef.Worksheets.Count];
int i = 0;
foreach (ExcelWorksheet ws in ef.Worksheets)
{
array[i] = ws.Name;
i++;
}

string text = "";

for (int j = 0; j < i; j++)
{
ExcelWorksheet sheet = ef.Worksheets[array[j]];

foreach (ExcelRow er in sheet.Rows)
{
foreach (ExcelCell ec in er.Cells)
{
if (ec.Value != null)
text += ec.Value.ToString() + " ";

}
text += "\n";
}
text += "\n\n";
}
//for Get Rich Text
string text = "";

for (int j = 0; j < i; j++)
{
ExcelWorksheet sheet = ef.Worksheets[array[j]];

foreach (ExcelRow er in sheet.Rows)
{
foreach (ExcelCell ec in er.Cells)
{
if (ec.Value != null)
{
text += ec.Value.ToString() + "\n(";
text += ec.Style.Font.Name + "," + ec.Style.Font.Color.ToString() + ")\n";
}

}
text += "\n\n";
}
text += "\n\n\n";
}


好的,所以我采取了不同的方法,采取我需要的数据,并把它放到剪贴板,但我似乎无法把它放在我想要的行。它从我使用的最后一个单元格开始





< pre lang =midl> //将字符串复制到Windows剪贴板。

string sData =& quot; 1 \tStart \tOpen\tTest1 \tExecute\tIntertop \t25 \ tStopChecklist;

系统。 Windows.Forms.Clipboard.SetDataObject(sData);







workSheet_range.get_Range(& quot ; A3:N15& quot;,Type.Missing);

str = Convert.ToString(r);



worksheet.Paste( workSheet_range,false);
ok, so i have taken a different approach by taking the data i need and putting it onto clipboard but i cant seem to put it in the row i want to. its starting from the last cell i used


<pre lang="midl">// Copy a string to the Windows clipboard.
string sData = &quot;1\tStart\tOpen\tTest1\tExecute\tIntertop\t25\tStopChecklist;
System.Windows.Forms.Clipboard.SetDataObject(sData);



workSheet_range.get_Range(&quot;A3:N15&quot;,Type.Missing);
str = Convert.ToString(r);

worksheet.Paste(workSheet_range,false);


你可以在这里找到更多的想法

http://www.codeproject.com/KB/cs/Excel_Application_in_C_.aspx [ ^ ]
You may find more ideas here
http://www.codeproject.com/KB/cs/Excel_Application_in_C_.aspx[^]


这篇关于读取Excel文件并生成包含其他信息的新Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆