내 양식에 DataGridView
이 있습니다. 내가 뭘하려고하면 사용자가 행을 선택하고 해당 행에서 Button(button_1)
데이터를 누르면 단어 문서를 보내고 column[i]
에 따라 숫자를 바꿔야합니다. 이제 코드를 사용하여
문제 1 하나의 행을 선택하고 버튼을 클릭하면 데이터가 Word 파일의 번호를 찾은 다음 바꿉니다. 그러나 예를 들어 "1"과 같은 모든 항목을 바꿉니다. 내가 각 행에 대해 그것을하고 싶어하기 때문에 한 번했다. 문제 2 사용자가 둘 이상의 행을 선택한 경우 마지막으로 선택된 행 데이터 만 내보내집니다. 어떤 아이디어 ??C# 데이터 행을 Word로 내보내기
private void button1_Click(object sender, EventArgs e)
{
string SendPath = "";
if (openFileDialogWord.ShowDialog(this) == DialogResult.OK)
{
SendPath = (openFileDialogWord.InitialDirectory + openFileDialogWord.FileName).ToString();
}
WordDoc(SendPath);
}
public void WordDoc(string getfilename)
{
object FileName = getfilename; //The filepath goes here
//Create word Application Object
Word.Application word = new Word.Application();
//Create word document Object
Word.Document doc = null;
//Create word Missing Object
object missing = System.Type.Missing;
object readOnly = false;
object isVisible = false;
// make visible Word application
word.Visible = true;
try
{
doc = word.Documents.Open(ref FileName, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
doc.Activate();
string Column1;
string Column2;
foreach (DataGridViewRow rows in dataGridView1.SelectedRows)
{
Column1 = rows.Cells[1].Value.ToString();
Column2 = rows.Cells[2].Value.ToString();
this.FindAndReplace(word, "1", Column1);
this.FindAndReplace(word, "2", Column2);
}
MessageBox.Show("Complete");
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
private void FindAndReplace(Word.Application word, object findText, object replaceText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
word.Selection.Find.Execute(ref findText, ref matchCase,
ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
ref matchAllWordForms, ref forward, ref wrap, ref format,
ref replaceText, ref replace, ref matchKashida,
ref matchDiacritics,
ref matchAlefHamza, ref matchControl);
}
누군가가 ???????? – user2345661
이 부분에 문제가 있습니다. object replace = 2; DreaminCode에서 해결책을 찾았습니다. 대체 된 개체 replace = 2; 오브젝트 대체 = 1; 그리고 그것은 완벽하게 일하고 있습니다. – user2345661