Excelワークシートを別々のファイルに分割するC#.NET関数を作成しようとしています。私はExcel Interopを使用していますが、私が望むように動作させることはできません。私ができることは、特定のタブを選択してファイルを再保存することです。 MSDNのドキュメントは、どの機能を使用するかについては非常に不明瞭です。私は、これらの機能のいくつかが期待どおりに動作していないことを他の場所で読んでいます。Excelタブを分割してファイルを分割する
private void SplitFile(string targetPath, string sourceFile)
{
Excel.Application xlApp;
Excel.Workbook xlFile;
//Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
string exportFormat = "";
if (cboExcel.Checked == true) //set the output format
exportFormat = "XLSX";
else if (cboCsv.Checked == true)
exportFormat = "CSV";
else
Console.WriteLine("Error detecting output format");
xlApp = new Excel.Application(); //object for controlling Excel
xlFile = xlApp.Workbooks.Open(txtFilePath.Text, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); //open the source file
//xlWorkSheet = (Excel.Worksheet)xlFile.Worksheets.get_Item(1); //Select the first tab in the file. Note: Index does NOT start are zero.
xlApp.DisplayAlerts = false; //override Excel save dialog message
int TabCount = xlFile.Worksheets.Count; //total count of the tabs in the file
int sheetCount = 0; //this will be used to output the number of exported sheets
for (int i = 1; i <= TabCount; i++) //for each sheet in the workbook...
{
//Console.WriteLine(i.ToString() + ": " + xlFile.Worksheets.Item[i].Name);
xlApp.ActiveWorkbook.Sheets[i].Select();
string sheetName = xlFile.Sheets[i].Name; //..get the name of the sheet. It will be used for the new filename
Console.WriteLine(i.ToString() + ": " + sheetName);
string newFilename = targetPath + "\\" + sheetName; //set the filename with full path, but no extension
Console.WriteLine(newFilename);
toolStripStatus.Text = "Exporting: " + sheetName; //update the status bar
Excel.Worksheet tempSheet = (xlApp.Worksheets[i]); //Current tab will be saved to this in a new workbook
tempSheet.Copy();
Excel.Workbook tempBook = xlApp.ActiveWorkbook;
try
{
switch (exportFormat) //if the file does NOT exist OR if does and the the user wants to overwrite it, do the export and increase the sheetCount by 1
{
case "CSV":
if (!File.Exists(newFilename + ".csv") || MessageBox.Show(sheetName + ".csv already exists. Overwrite?", "Confirm Overwrite", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
tempBook.Worksheets[1].SaveAs(newFilename, Excel.XlFileFormat.xlCSV, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing);
sheetCount++;
}
break;
case "XLSX":
if (!File.Exists(newFilename + ".xlsx") || MessageBox.Show(sheetName + ".xlsx already exists. Overwrite?", "Confirm Overwrite", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
tempBook.Worksheets[1].SaveAs(newFilename, Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing);
//tempSheet.SaveAs(newFilename, Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing);
sheetCount++;
}
break;
default:
Console.WriteLine("Unexpected export format");
MessageBox.Show("Unexpected export format");
break;
}
}
catch (Exception ex)
{
toolStripStatus.Text = "Error!";
string errorMessage = "Error Exporting " + sheetName + System.Environment.NewLine + "Original Message: " + ex.Message;
MessageBox.Show(errorMessage, "Error Exporting", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Console.WriteLine(errorMessage);
toolStripStatus.Text = "Ready";
break;
}
}
//Closing Processes Start ===================================================
toolStripStatus.Text = "Process Finished";
xlApp.DisplayAlerts = true;
xlFile.Close(true, misValue, misValue);
xlApp.Quit();
Console.WriteLine(sheetCount.ToString() + " files exported.");
MessageBox.Show(sheetCount.ToString() + " files exported.","Process Complete", MessageBoxButtons.OK ,MessageBoxIcon.Information);
toolStripStatus.Text = "Ready";
//Closing Processes Finish ====================================================================================================
ターゲットファイルをファイルごとに複数のファイルに分割すると予想されます。
上記のコードを使用すると、選択したタブだけで同じファイルコピーを取得できます。私はそれ以来、上記のバリエーションをいくつか試しましたが、何もうまくいきません。
あなたはより多くの情報を提供する必要があります。あなたが今やっていること、そしてうまくいかないこと(あなたが期待している結果と比べて得られる結果)を見せてください。 –