|
|
|
|||||||
| Знаете ли Вы, что ... | |
| ...инструкция по установке аватара описана в Правилах форума. | |
| << Предыдущий совет - Случайный совет - Следующий совет >> | |
| .NET & ASP.NET Вопросы программирования .NET, ASP.NET, Web Services и других WEB-технологий .NET. |
| Ответить |
|
|
Опции темы | Опции просмотра |
|
|
#12 | ||
![]() |
ХМ мы использовали стандартную библиотеку... и работали через неё с файлом ворда. Вот приблизительное описание кода:
Код:
using System.Reflection;
using Word; // Interop.Word
object missing = Missing.Value;
ApplicationClass Word_App = null;
Document Word_doc = null;
try
{
Word_App = new ApplicationClass();
Word_doc = new Document();
}
catch (Exception e)
{
MessageBox.Show("Can't create a word document " + e.ToString(), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
goto Exit;//не сочтите за глупость таков код из MSDN
}
AutoCorrect autocorrect = Word_App.AutoCorrect;
//Путь до файла шаблона.
object Name = @"\Doc\NAME.dot";
object Vis = true;
Word_App.Visible = true;
Word_doc = Word_App.Documents.Add(ref Name, ref missing, ref missing, ref Vis);
//Обращаемся к таблице шаблона
Word_doc.Tables.Item(1).Cell(1, 1).Range.Text = "VALUES";
try
{
Word_App.ActiveWindow.Selection.Start = 0;
Word_App.ActiveWindow.Selection.End = 0;
Word_App.Activate();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
Word_App.Quit(ref missing, ref missing, ref missing);
}
Exit: ;
|
||
|
|
Ответить |
|
|
#13 |
|
Stafar Employment
старший тестировщик
Сообщений: 31
+ 5
5/4
– 9
5/4
![]() |
Вот выписал кусочек кода, использовал для экспорта данных с датагрида в Excel файл
Код:
using Excel = Microsoft.Office.Interop.Excel;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel files|*.xls";
sfd.Title = "Выберите директорию для сохранения файла отчёта";
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
GetFullRptNotCompCellsNum();
CalculateDups();
Process[] process1 = Process.GetProcessesByName("EXCEL");
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
((Excel.Range)xlWorkSheet.Cells).NumberFormat = "@";
((Excel.Range)xlWorkSheet.Cells[1, 1]).ColumnWidth = 15;
((Excel.Range)xlWorkSheet.Cells[1, 2]).ColumnWidth = 20;
((Excel.Range)xlWorkSheet.Cells[1, 3]).ColumnWidth = 25;
((Excel.Range)xlWorkSheet.Cells[1, 4]).ColumnWidth = 25;
((Excel.Range)xlWorkSheet.Cells[1, 5]).ColumnWidth = 15;
((Excel.Range)xlWorkSheet.Cells[1, 6]).ColumnWidth = 15;
((Excel.Range)xlWorkSheet.Cells[1, 1]).Font.Bold = true;
xlWorkSheet.Cells[carriage, 1] = gv_Left.Rows[1].Cells[1].Value.ToString();
xlWorkSheet.Cells[1e, 2] = gv_Right.Rows[1].Cells[2].Value.ToString();
xlWorkSheet.Cells[carriage, 3] = "test";
xlWorkSheet.Cells[1, 4] = "test";
xlWorkSheet.Cells[carriage, 5] = " ";
((Excel.Range)xlWorkSheet.Cells[carriage, 1]).Cells.Borders.LineStyle = BorderStyle.FixedSingle;
((Excel.Range)xlWorkSheet.Cells[carriage, 2]).Cells.Borders.LineStyle = BorderStyle.FixedSingle;
((Excel.Range)xlWorkSheet.Cells[carriage, 3]).Cells.Borders.LineStyle = BorderStyle.FixedSingle;
((Excel.Range)xlWorkSheet.Cells[carriage, 4]).Cells.Borders.LineStyle = BorderStyle.FixedSingle;
string reportFile = sfd.FileName;// Directory.GetCurrentDirectory() + @"\Report.xls";
xlWorkBook.SaveAs(reportFile, Excel.XlFileFormat.xlWorkbookNormal,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Process[] process2 = Process.GetProcessesByName("EXCEL");
process2[process1.Length].Kill();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Отчёт создан успешно!", "Готово!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
|
Ответить |
|